[git commit] ash: fix "wait -n" behavior wrt already-exited jobs

Denys Vlasenko vda.linux at googlemail.com
Wed Oct 7 09:15:37 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=30f2f883bdc4fd8c8f44cc6985c77ab1809a6a0d
branch: https://git.busybox.net/busybox/log/?h=master

function                                             old     new   delta
waitcmd                                              318     346     +28
kill_main                                            907     919     +12
dowait                                               553     557      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 44/0)               Total: 44 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c                                        | 43 +++++++++++++---------
 .../ash-misc/wait_does_not_return_on_stops.right   |  7 ++++
 .../ash-misc/wait_does_not_return_on_stops.tests   |  9 +++++
 .../wait_n_waits_for_already_waited_for_job.tests  |  2 +-
 ..._n_waits_for_many_already_waited_for_jobs.right |  3 ++
 ..._n_waits_for_many_already_waited_for_jobs.tests |  9 +++++
 .../hush-misc/wait_does_not_return_on_stops.right  |  7 ++++
 .../hush-misc/wait_does_not_return_on_stops.tests  |  9 +++++
 8 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index e1c37f808..789331f33 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -4480,9 +4480,10 @@ static int waitone(int block, int *status, struct job *job)
 
 #if BASH_WAIT_N
 	if (want_jobexitstatus) {
-		if (thisjob && thisjob->state == JOBDONE)
+		if (thisjob && thisjob->state == JOBDONE) {
 			*status = thisjob->ps[thisjob->nprocs - 1].ps_status;
-		else {
+			thisjob->waited = 1;
+		} else {
 			/* The job is not found, or not fully completed:
 			 * "wait -n" should NOT yet finish. Giving it -1, it will retry. */
 			*status = -1;
@@ -4864,7 +4865,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
 	int status;
 	char wait_n = nextopt("n");
 #else
-	const int wait_n = 0;
+	int wait_n = 0; /* never changes if !BASH_WAIT_N */
 	nextopt(nullstr);
 #endif
 	retval = 0;
@@ -4875,14 +4876,28 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
 		for (;;) {
 			jp = curjob;
 			while (1) {
-				if (!jp) { /* no running procs */
-					if (wait_n) /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */
+				if (!jp) { /* no more jobs */
+					if (wait_n == 1)
+						break; /* there is a live job */
+					if (wait_n == 'n') /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */
 						retval = 127;
 					goto ret;
 				}
-				if (jp->state == JOBRUNNING)
-					break;
-				jp->waited = 1;
+				if (!wait_n) {
+					if (jp->state != JOBDONE)
+						break;
+					jp->waited = 1;
+				} else {
+					if (jp->state != JOBDONE) {
+						wait_n = 1; /* replace 'n': mark that we found a live job */
+					} else if (!jp->waited) {
+						jp->waited = 1;
+						/* "wait -n" returns status of unwaited-for dead jobs immediately */
+						/* (bash-5.3.9 does this in -c 'CMDs' and scripts only) */
+						retval = getstatus(jp);
+						goto ret;
+					}
+				}
 				jp = jp->prev_job;
 			}
 	/* man bash:
@@ -4911,7 +4926,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
 				 */
 				if (pid > 0) { /* some process did exit */
 					if (status != -1) {
-# if JOBS /* only possible if JOBS */
+# if JOBS /* processes can be stopped even if !JOBS, but we never wait for them with WUNTRACED - never see stops */
 						if (WIFSTOPPED(status))
 							continue; /* not interested in stops, find another process */
 # endif
@@ -4921,14 +4936,6 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
 						goto ret;
 					}
 					/* else: not entire job has exited (only a process), wait more */
-				} else {
-					/* There are no alive jobs. Are there dead ones? */
-					/* (bash-5.3.9 does this) */
-					if (curjob) {
-						retval = getstatus(curjob);
-						freejob(curjob);
-						goto ret;
-					}
 				}
 			}
 #endif
@@ -4950,7 +4957,7 @@ waitcmd(int argc UNUSED_PARAM, char **argv)
 		} else {
 			job = getjob(*argv, 0);
 		}
-		/* loop until process terminated or stopped */
+		/* loop until process terminated (but stops do not count) */
 		dowait(DOWAIT_CHILD_OR_SIG, /*status:*/ NULL, job);
 		if (pending_sig)
 			goto sigout;
diff --git a/shell/ash_test/ash-misc/wait_does_not_return_on_stops.right b/shell/ash_test/ash-misc/wait_does_not_return_on_stops.right
new file mode 100644
index 000000000..6df40cf09
--- /dev/null
+++ b/shell/ash_test/ash-misc/wait_does_not_return_on_stops.right
@@ -0,0 +1,7 @@
+wait1
+INT
+w1:130
+wait2
+INT
+w2:130
+Ok:0
diff --git a/shell/ash_test/ash-misc/wait_does_not_return_on_stops.tests b/shell/ash_test/ash-misc/wait_does_not_return_on_stops.tests
new file mode 100755
index 000000000..31a055b48
--- /dev/null
+++ b/shell/ash_test/ash-misc/wait_does_not_return_on_stops.tests
@@ -0,0 +1,9 @@
+trap 'echo INT' INT
+(sleep 2;sh -c 'kill -sstop $$') &
+bg=$!
+(sleep 1;kill -INT $$;sleep 2;kill -INT $$) &
+echo wait1; wait; echo w1:$?
+echo wait2; wait; echo w2:$?
+kill -cont $bg
+kill $bg
+echo Ok:$?
diff --git a/shell/ash_test/ash-misc/wait_n_waits_for_already_waited_for_job.tests b/shell/ash_test/ash-misc/wait_n_waits_for_already_waited_for_job.tests
index b41294bec..0117bbd70 100755
--- a/shell/ash_test/ash-misc/wait_n_waits_for_already_waited_for_job.tests
+++ b/shell/ash_test/ash-misc/wait_n_waits_for_already_waited_for_job.tests
@@ -6,10 +6,10 @@ echo "Sent SIGTERM:$?"
 
 # The internal state depends on whether shell manages to see the death of $bg before entering "wait -n":
 # will the job be already "dead and waited-for"? In the latter case, the correctness is harder to achieve.
+sleep 0.1
 echo "Test: wait -n exit code for sigkilled (and possibly already waited-for) pipe"
 wait -n
 echo "wait -n #1: $? (should be 143 = 128+SIGTERM)"
 echo "Test: wait -n exits 127 if nothing to wait for"
 wait -n
 echo "wait -n #1: $? (should be 127)"
-
diff --git a/shell/ash_test/ash-misc/wait_n_waits_for_many_already_waited_for_jobs.right b/shell/ash_test/ash-misc/wait_n_waits_for_many_already_waited_for_jobs.right
new file mode 100644
index 000000000..843cfea50
--- /dev/null
+++ b/shell/ash_test/ash-misc/wait_n_waits_for_many_already_waited_for_jobs.right
@@ -0,0 +1,3 @@
+4:4
+8:8
+127:127
diff --git a/shell/ash_test/ash-misc/wait_n_waits_for_many_already_waited_for_jobs.tests b/shell/ash_test/ash-misc/wait_n_waits_for_many_already_waited_for_jobs.tests
new file mode 100755
index 000000000..0120d2ade
--- /dev/null
+++ b/shell/ash_test/ash-misc/wait_n_waits_for_many_already_waited_for_jobs.tests
@@ -0,0 +1,9 @@
+#(sleep 0.2;exit 2) &
+#^^^ bash will report jobs in order of them exiting, we do the reverse order. fix?
+(sleep 0.4;exit 4) &
+(sleep 0.8;exit 8) &
+sleep 0.6
+#wait -n; echo 2:$?
+wait -n; echo 4:$?
+wait -n; echo 8:$?
+wait -n; echo 127:$?
diff --git a/shell/hush_test/hush-misc/wait_does_not_return_on_stops.right b/shell/hush_test/hush-misc/wait_does_not_return_on_stops.right
new file mode 100644
index 000000000..6df40cf09
--- /dev/null
+++ b/shell/hush_test/hush-misc/wait_does_not_return_on_stops.right
@@ -0,0 +1,7 @@
+wait1
+INT
+w1:130
+wait2
+INT
+w2:130
+Ok:0
diff --git a/shell/hush_test/hush-misc/wait_does_not_return_on_stops.tests b/shell/hush_test/hush-misc/wait_does_not_return_on_stops.tests
new file mode 100755
index 000000000..31a055b48
--- /dev/null
+++ b/shell/hush_test/hush-misc/wait_does_not_return_on_stops.tests
@@ -0,0 +1,9 @@
+trap 'echo INT' INT
+(sleep 2;sh -c 'kill -sstop $$') &
+bg=$!
+(sleep 1;kill -INT $$;sleep 2;kill -INT $$) &
+echo wait1; wait; echo w1:$?
+echo wait2; wait; echo w2:$?
+kill -cont $bg
+kill $bg
+echo Ok:$?


More information about the busybox-cvs mailing list