			     BASH PATCH REPORT
			     =================

Bash-Release:	5.2
Patch-ID:	bash52-019

Bug-Reported-by:	Steffen Nurpmeso <steffen@sdaoden.eu>
Bug-Reference-ID:	<20230116233547.2jFxL%steffen@sdaoden.eu>
Bug-Reference-URL:	https://lists.gnu.org/archive/html/bug-bash/2023-01/msg00057.html

Bug-Description:

There are some cases where the shell reaped a background (asynchronous) job
and would incorrectly try to set the terminal's process group back to the
shell's. In these cases it never set the terminal process group to that
jobs's process group initially, so resetting it is incorrect.

Patch (apply with `patch -p0'):

*** ../bash-5.2-patched/jobs.c	2022-12-13 12:09:02.000000000 -0500
--- jobs.c	2023-10-26 12:12:10.000000000 -0400
***************
*** 3078,3084 ****
  	 subshell.  Make sure subst.c:command_substitute uses the same
  	 conditions to determine whether or not it should undo this and
! 	 give the terminal to pipeline_pgrp. */
!       
        if ((flags & JWAIT_NOTERM) == 0 && running_in_background == 0 &&
  	  (subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0)
  	give_terminal_to (shell_pgrp, 0);
--- 3036,3046 ----
  	 subshell.  Make sure subst.c:command_substitute uses the same
  	 conditions to determine whether or not it should undo this and
! 	 give the terminal to pipeline_pgrp. We don't give the terminal
! 	 back to shell_pgrp if an async job in the background exits because
! 	 we never gave it to that job in the first place. An async job in
! 	 the foreground is one we started in the background and foregrounded
! 	 with `fg', and gave it the terminal. */
        if ((flags & JWAIT_NOTERM) == 0 && running_in_background == 0 &&
+ 	  (job == NO_JOB || IS_ASYNC (job) == 0 || IS_FOREGROUND (job)) &&
  	  (subshell_environment & (SUBSHELL_ASYNC|SUBSHELL_PIPE)) == 0)
  	give_terminal_to (shell_pgrp, 0);
***************
*** 3624,3627 ****
--- 3599,3603 ----
        get_tty_state ();
        save_stty = shell_tty_info;
+       jobs[job]->flags &= ~J_ASYNC;	/* no longer async */
        /* Give the terminal to this job. */
        if (IS_JOBCONTROL (job))

*** ../bash-5.2/patchlevel.h	2020-06-22 14:51:03.000000000 -0400
--- patchlevel.h	2020-10-01 11:01:28.000000000 -0400
***************
*** 26,30 ****
     looks for to find the patch level (for the sccs version string). */
  
! #define PATCHLEVEL 18
  
  #endif /* _PATCHLEVEL_H_ */
--- 26,30 ----
     looks for to find the patch level (for the sccs version string). */
  
! #define PATCHLEVEL 19
  
  #endif /* _PATCHLEVEL_H_ */
