Skip to content

Commit e775c7f

Browse files
committed
fix(subprocess): a child that is exiting at the group check is a finished run
macOS answers ESRCH to setpgid and getpgid for our own unreaped child from the moment it starts to exit, before waitid can report it. a5c7ae9 accepted a child that had already exited, probed with waitid(WNOHANG|WNOWAIT); in the window before the child is reapable that probe said "not exited", and the spawn was reported as SPAWN_FAILED errno 3. macos-15-intel CI kept hitting it in a different Git-backed fixture on each run; with the new diagnostics it named itself: `git config --local core.protectNTFS false`, outcome=5 errno=3. A child of ours that setpgid/getpgid cannot find is not running: it has exited or is exiting. The parent now waits until it is reapable (WNOWAIT leaves it for the supervision, which reaps it with its real exit status); the wait ends with the child's exit. EACCES/EPERM keep the group check. A test seam makes the parent observe the exiting state (ESRCH, not yet reapable): subprocess_run_child_exiting_at_group_check is SPAWN_FAILED without this change and exit status 3 with it. Signed-off-by: Martin Vogel <[email protected]>
1 parent 35605c5 commit e775c7f

3 files changed

Lines changed: 64 additions & 13 deletions

File tree

‎src/foundation/subprocess.c‎

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,10 @@ static int g_hold_until_exit = 0;
11241124
void cbm_subprocess_hold_parent_until_child_exits_for_testing(int spawns) {
11251125
g_hold_until_exit = spawns > 0 ? spawns : 0;
11261126
}
1127+
static int g_observe_exiting = 0;
1128+
void cbm_subprocess_observe_exiting_child_for_testing(int spawns) {
1129+
g_observe_exiting = spawns > 0 ? spawns : 0;
1130+
}
11271131
int cbm_subprocess_pending_spawn_eagain_for_testing(void) {
11281132
return g_force_spawn_eagain;
11291133
}
@@ -1400,12 +1404,22 @@ static int cbm_posix_spawn_apple(cbm_subprocess_t *process, int input, int outpu
14001404
}
14011405
#endif
14021406

1403-
/* The child exited and waits to be reaped (WNOWAIT leaves it for the
1404-
* supervision to collect with its exit status). */
1405-
static bool cbm_posix_child_exited(pid_t pid) {
1406-
siginfo_t info;
1407-
memset(&info, 0, sizeof(info));
1408-
return waitid(P_PID, (id_t)pid, &info, WEXITED | WNOHANG | WNOWAIT) == 0 && info.si_pid == pid;
1407+
/* Our own unreaped child that setpgid/getpgid cannot find (ESRCH) is not
1408+
* running: it has exited, or is exiting -- macOS stops finding a child as it
1409+
* starts to exit, before waitid can report it. Wait until it is reapable;
1410+
* the wait ends with its exit, and WNOWAIT leaves it for the supervision to
1411+
* collect with its exit status. */
1412+
static bool cbm_posix_child_reapable(pid_t pid) {
1413+
for (;;) {
1414+
siginfo_t info;
1415+
memset(&info, 0, sizeof(info));
1416+
if (waitid(P_PID, (id_t)pid, &info, WEXITED | WNOWAIT) == 0) {
1417+
return info.si_pid == pid;
1418+
}
1419+
if (errno != EINTR) {
1420+
return false;
1421+
}
1422+
}
14091423
}
14101424

14111425
static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
@@ -1543,15 +1557,30 @@ static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
15431557
#endif
15441558
/* Parent and child both establish the group, removing scheduler-order races.
15451559
* If the child won and already execed, EACCES is accepted only after proving
1546-
* that its process group is the expected isolated one. A child that already
1547-
* EXITED is a finished run, not a failed spawn: macOS no longer shows an
1548-
* unreaped child to setpgid/getpgid (ESRCH), and on a loaded machine a short
1549-
* command (`git write-tree`) finishes before the parent gets here. Its group
1550-
* was set before exec, by the spawn attributes or by the child itself; the
1560+
* that its process group is the expected isolated one. A child that has
1561+
* EXITED, or is exiting, is a finished run, not a failed spawn: macOS no
1562+
* longer shows such an unreaped child to setpgid/getpgid (ESRCH), and on a
1563+
* loaded machine a short command (`git config`) gets there before the
1564+
* parent does. Its group was set before exec, by the spawn attributes or by
1565+
* the child itself; the parent waits until it is reapable, and the
15511566
* supervision below reaps it with its real exit status. */
1552-
bool contained = setpgid(pid, pid) == 0;
1567+
bool observe_exiting = false;
1568+
#ifdef CBM_ENABLE_TEST_SEAMS
1569+
if (g_observe_exiting > 0) {
1570+
g_observe_exiting--;
1571+
observe_exiting = true;
1572+
}
1573+
#endif
1574+
bool contained = !observe_exiting && setpgid(pid, pid) == 0;
1575+
if (observe_exiting) {
1576+
errno = ESRCH;
1577+
}
15531578
if (!contained && (errno == EACCES || errno == EPERM || errno == ESRCH)) {
1554-
contained = getpgid(pid) == pid || cbm_posix_child_exited(pid);
1579+
bool missing = errno == ESRCH;
1580+
contained = !observe_exiting && getpgid(pid) == pid;
1581+
if (!contained && (missing || errno == ESRCH)) {
1582+
contained = cbm_posix_child_reapable(pid);
1583+
}
15551584
}
15561585
if (!contained) {
15571586
(void)kill(pid, SIGKILL);

‎src/foundation/subprocess.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ int cbm_subprocess_pending_spawn_eagain_for_testing(void);
211211
* machine produces when a short command finishes before the parent runs
212212
* again. Test builds only. */
213213
void cbm_subprocess_hold_parent_until_child_exits_for_testing(int spawns);
214+
/* For the next N POSIX spawns, the parent's group check observes what macOS
215+
* shows for a child that is EXITING: setpgid and getpgid answer ESRCH while
216+
* waitid does not report it yet. Test builds only. */
217+
void cbm_subprocess_observe_exiting_child_for_testing(int spawns);
214218
#endif
215219

216220
/* How the POSIX fork+exec child closed its inherited descriptors (#1484). */

‎tests/test_subprocess.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ TEST(subprocess_run_child_exited_before_group_check) {
155155
#endif
156156
}
157157

158+
/* The window before that: macOS answers ESRCH for a child that has started
159+
* exiting, before waitid can report it. A non-blocking "has it exited?"
160+
* probe said no there, and macos-15-intel CI read a short `git config` as
161+
* SPAWN_FAILED errno 3. The seam makes the parent observe exactly that. */
162+
TEST(subprocess_run_child_exiting_at_group_check) {
163+
#ifdef _WIN32
164+
SKIP_PLATFORM("POSIX process groups");
165+
#else
166+
cbm_subprocess_observe_exiting_child_for_testing(1);
167+
cbm_proc_result_t r = run_sh("exit 3", 0);
168+
cbm_subprocess_observe_exiting_child_for_testing(0);
169+
ASSERT_EQ(r.outcome, CBM_PROC_EXIT_NONZERO);
170+
ASSERT_EQ(r.exit_code, 3);
171+
PASS();
172+
#endif
173+
}
174+
158175
/* Daemon background helpers intentionally invoke fixed tool names such as
159176
* `curl` and `git`. A shell-free spawn must still perform the normal PATH
160177
* lookup for a name without a directory separator; exact binary paths keep
@@ -2346,6 +2363,7 @@ SUITE(subprocess) {
23462363
RUN_TEST(subprocess_run_clean);
23472364
RUN_TEST(subprocess_run_exit_nonzero);
23482365
RUN_TEST(subprocess_run_child_exited_before_group_check);
2366+
RUN_TEST(subprocess_run_child_exiting_at_group_check);
23492367
RUN_TEST(subprocess_run_resolves_literal_binary_name_from_path);
23502368
RUN_TEST(subprocess_run_crash_is_crash);
23512369
RUN_TEST(subprocess_run_hang_is_hang);

0 commit comments

Comments
 (0)