Skip to content

Commit a5c7ae9

Browse files
committed
fix(subprocess): a child that already exited is a finished run, not a failed spawn
After posix_spawn the parent re-asserts the child's process group. A short command can exit before the parent gets there; macOS then answers ESRCH for the exited, unreaped child to both setpgid and getpgid, and the run was reported as SPAWN_FAILED (errno 3). macOS CI read `git write-tree` that way under load (macos-15-intel and the LeakSanitizer leg). The group was already set before exec, by the spawn attributes or by the child itself, so an exited child is accepted when waitid(WNOWAIT) shows it waiting to be reaped; the supervision then collects its real exit status. A test seam holds the parent until the child has exited, which makes that order deterministic: subprocess_run_child_exited_before_group_check is red without the fix (SPAWN_FAILED) and green with it, on macOS and Linux. The test file is also reflowed to the project's clang-format, as on main. Signed-off-by: Martin Vogel <[email protected]>
1 parent 6893b52 commit a5c7ae9

3 files changed

Lines changed: 82 additions & 31 deletions

File tree

‎src/foundation/subprocess.c‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,10 @@ static int g_force_spawn_eagain = 0;
11201120
void cbm_subprocess_force_spawn_eagain_for_testing(int attempts) {
11211121
g_force_spawn_eagain = attempts > 0 ? attempts : 0;
11221122
}
1123+
static int g_hold_until_exit = 0;
1124+
void cbm_subprocess_hold_parent_until_child_exits_for_testing(int spawns) {
1125+
g_hold_until_exit = spawns > 0 ? spawns : 0;
1126+
}
11231127
int cbm_subprocess_pending_spawn_eagain_for_testing(void) {
11241128
return g_force_spawn_eagain;
11251129
}
@@ -1396,6 +1400,14 @@ static int cbm_posix_spawn_apple(cbm_subprocess_t *process, int input, int outpu
13961400
}
13971401
#endif
13981402

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;
1409+
}
1410+
13991411
static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
14001412
int input_flags = O_RDONLY;
14011413
#ifdef O_CLOEXEC
@@ -1522,12 +1534,24 @@ static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
15221534
(void)close(stdout_capture);
15231535
}
15241536

1537+
#ifdef CBM_ENABLE_TEST_SEAMS
1538+
if (g_hold_until_exit > 0) {
1539+
g_hold_until_exit--;
1540+
siginfo_t held;
1541+
(void)waitid(P_PID, (id_t)pid, &held, WEXITED | WNOWAIT);
1542+
}
1543+
#endif
15251544
/* Parent and child both establish the group, removing scheduler-order races.
15261545
* If the child won and already execed, EACCES is accepted only after proving
1527-
* that its process group is the expected isolated one. */
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
1551+
* supervision below reaps it with its real exit status. */
15281552
bool contained = setpgid(pid, pid) == 0;
15291553
if (!contained && (errno == EACCES || errno == EPERM || errno == ESRCH)) {
1530-
contained = getpgid(pid) == pid;
1554+
contained = getpgid(pid) == pid || cbm_posix_child_exited(pid);
15311555
}
15321556
if (!contained) {
15331557
(void)kill(pid, SIGKILL);

‎src/foundation/subprocess.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ bool cbm_build_win_cmd_payload(char *buf, size_t cap, const char *cmd_executable
206206
* of hoping a loaded machine reproduces it. Test builds only. */
207207
void cbm_subprocess_force_spawn_eagain_for_testing(int attempts);
208208
int cbm_subprocess_pending_spawn_eagain_for_testing(void);
209+
/* Hold the parent of the next N POSIX spawns until the child has exited (left
210+
* unreaped) before it checks the child's process group: the order a loaded
211+
* machine produces when a short command finishes before the parent runs
212+
* again. Test builds only. */
213+
void cbm_subprocess_hold_parent_until_child_exits_for_testing(int spawns);
209214
#endif
210215

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

‎tests/test_subprocess.c‎

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ TEST(subprocess_run_exit_nonzero) {
137137
#endif
138138
}
139139

140+
/* A short command can finish before its parent checks the child's process
141+
* group; macOS then answers ESRCH for the exited, unreaped child. That is a
142+
* finished run with its own exit status, never a spawn failure (macOS CI read
143+
* `git write-tree` as SPAWN_FAILED errno 3 under load). The seam holds the
144+
* parent until the child has exited: that order, deterministically. */
145+
TEST(subprocess_run_child_exited_before_group_check) {
146+
#ifdef _WIN32
147+
SKIP_PLATFORM("POSIX process groups");
148+
#else
149+
cbm_subprocess_hold_parent_until_child_exits_for_testing(1);
150+
cbm_proc_result_t r = run_sh("exit 3", 0);
151+
cbm_subprocess_hold_parent_until_child_exits_for_testing(0);
152+
ASSERT_EQ(r.outcome, CBM_PROC_EXIT_NONZERO);
153+
ASSERT_EQ(r.exit_code, 3);
154+
PASS();
155+
#endif
156+
}
157+
140158
/* Daemon background helpers intentionally invoke fixed tool names such as
141159
* `curl` and `git`. A shell-free spawn must still perform the normal PATH
142160
* lookup for a name without a directory separator; exact binary paths keep
@@ -1844,8 +1862,7 @@ static bool sp_stdin_clean(int rc, const cbm_proc_result_t *result) {
18441862
* may be destroyed, but a nonquiescent tree's fixture is intentionally retained. */
18451863
static bool sp_stdin_safe_run(int rc, const cbm_proc_result_t *result) {
18461864
return result->tree_quiesced ||
1847-
(rc == -1 && result->outcome == CBM_PROC_SPAWN_FAILED &&
1848-
!result->supervision_failed);
1865+
(rc == -1 && result->outcome == CBM_PROC_SPAWN_FAILED && !result->supervision_failed);
18491866
}
18501867

18511868
static bool sp_stdin_finish(cbm_subprocess_t *process, cbm_proc_result_t *result) {
@@ -1860,8 +1877,7 @@ static bool sp_stdin_finish(cbm_subprocess_t *process, cbm_proc_result_t *result
18601877
}
18611878

18621879
static int sp_stdin_run(cbm_proc_opts_t *opts, cbm_proc_result_t *result) {
1863-
const char *argv[] = {sp_stdout_test_binary, "__cbm_subprocess_stdin_probe", "echo", "",
1864-
NULL};
1880+
const char *argv[] = {sp_stdout_test_binary, "__cbm_subprocess_stdin_probe", "echo", "", NULL};
18651881
opts->bin = sp_stdout_test_binary;
18661882
opts->argv = argv;
18671883
opts->quiet_timeout_ms = 10000; /* Finite containment guard, not a timing oracle. */
@@ -1884,12 +1900,10 @@ static sp_stdin_rejection_t sp_stdin_check_rejection(const char *binary, const c
18841900
const char *valid_empty_input) {
18851901
sp_stdin_rejection_t check = {.safe_cleanup = true};
18861902
(void)cbm_unlink(marker);
1887-
check.prepared = sp_stdin_write(out, sp_stdin_out_sentinel,
1888-
sizeof(sp_stdin_out_sentinel) - 1) &&
1889-
sp_stdin_write(log, sp_stdin_log_sentinel,
1890-
sizeof(sp_stdin_log_sentinel) - 1) &&
1891-
sp_stdout_file_equals(valid_empty_input, "", 0) &&
1892-
!cbm_file_exists(marker);
1903+
check.prepared =
1904+
sp_stdin_write(out, sp_stdin_out_sentinel, sizeof(sp_stdin_out_sentinel) - 1) &&
1905+
sp_stdin_write(log, sp_stdin_log_sentinel, sizeof(sp_stdin_log_sentinel) - 1) &&
1906+
sp_stdout_file_equals(valid_empty_input, "", 0) && !cbm_file_exists(marker);
18931907
if (!check.prepared)
18941908
return check;
18951909
const char *argv[] = {binary, "__cbm_subprocess_stdin_probe", "stamp", marker, NULL};
@@ -1922,8 +1936,7 @@ static sp_stdin_rejection_t sp_stdin_check_rejection(const char *binary, const c
19221936
cbm_proc_result_t result = {0};
19231937
rc = cbm_subprocess_run(&opts, &result);
19241938
check.safe_cleanup = sp_stdin_safe_run(rc, &result);
1925-
check.reusable = sp_stdin_clean(rc, &result) &&
1926-
sp_stdout_file_equals(out, "", 0) &&
1939+
check.reusable = sp_stdin_clean(rc, &result) && sp_stdout_file_equals(out, "", 0) &&
19271940
sp_stdout_file_equals(log, sp_stderr_text, sizeof(sp_stderr_text) - 1) &&
19281941
sp_stdout_file_equals(valid_empty_input, "", 0);
19291942
return check;
@@ -1938,16 +1951,15 @@ static int sp_stdin_probe(int argc, char **argv) {
19381951
return 101;
19391952
#endif
19401953
if (argc == 8 && strcmp(argv[2], "reject") == 0) {
1941-
sp_stdin_rejection_t check = sp_stdin_check_rejection(
1942-
argv[0], argv[3], argv[4], argv[5], argv[6], argv[7]);
1954+
sp_stdin_rejection_t check =
1955+
sp_stdin_check_rejection(argv[0], argv[3], argv[4], argv[5], argv[6], argv[7]);
19431956
if (!check.safe_cleanup)
19441957
return 125; /* Parent must retain files if nested containment failed. */
19451958
if (!check.prepared)
19461959
return 102;
19471960
if (!check.reusable)
19481961
return 103;
1949-
return check.rejected && check.no_handle && check.outputs_unchanged &&
1950-
check.no_child_marker
1962+
return check.rejected && check.no_handle && check.outputs_unchanged && check.no_child_marker
19511963
? 0
19521964
: 104;
19531965
}
@@ -1977,8 +1989,7 @@ static int sp_stdin_probe(int argc, char **argv) {
19771989
_lseek(cbm_fileno(stdin), 0, SEEK_SET) != 0)
19781990
return 107;
19791991
#else
1980-
if (read(cbm_fileno(stdin), &witness, 1) != 1 ||
1981-
lseek(cbm_fileno(stdin), 0, SEEK_SET) != 0)
1992+
if (read(cbm_fileno(stdin), &witness, 1) != 1 || lseek(cbm_fileno(stdin), 0, SEEK_SET) != 0)
19821993
return 107;
19831994
#endif
19841995
const char *child_argv[] = {argv[0], "__cbm_subprocess_stdin_probe", "echo", "", NULL};
@@ -2040,8 +2051,13 @@ TEST(subprocess_stdin_null_is_eof_even_with_readable_parent_input) {
20402051
unsigned char input[SP_STDIN_BYTES];
20412052
sp_stdin_payload(input);
20422053
bool prepared = sp_stdin_write(fx.other, input, sizeof(input));
2043-
const char *argv[] = {sp_stdout_test_binary, "__cbm_subprocess_stdin_probe", "parent-eof",
2044-
fx.other, fx.out, fx.log, NULL};
2054+
const char *argv[] = {sp_stdout_test_binary,
2055+
"__cbm_subprocess_stdin_probe",
2056+
"parent-eof",
2057+
fx.other,
2058+
fx.out,
2059+
fx.log,
2060+
NULL};
20452061
cbm_proc_opts_t opts = {.bin = sp_stdout_test_binary, .argv = argv, .quiet_timeout_ms = 20000};
20462062
cbm_proc_result_t result = {0};
20472063
int rc = prepared ? cbm_subprocess_run(&opts, &result) : -1;
@@ -2070,8 +2086,7 @@ TEST(subprocess_stdin_empty_regular_file_is_success) {
20702086
cbm_proc_result_t result = {0};
20712087
int rc = prepared ? sp_stdin_run(&opts, &result) : -1;
20722088
bool safe = !prepared || sp_stdin_safe_run(rc, &result);
2073-
bool exact = sp_stdout_file_equals(fx.out, "", 0) &&
2074-
sp_stdout_file_equals(fx.other, "", 0) &&
2089+
bool exact = sp_stdout_file_equals(fx.out, "", 0) && sp_stdout_file_equals(fx.other, "", 0) &&
20752090
sp_stdout_file_equals(fx.log, sp_stderr_text, sizeof(sp_stderr_text) - 1);
20762091
int cleanup = safe ? th_rmtree(fx.root) : -1;
20772092
ASSERT_EQ(cleanup, 0);
@@ -2182,9 +2197,8 @@ TEST(subprocess_stdin_caller_path_mutation_before_gate_release) {
21822197
bool safe = !process || sp_stdin_finish(process, &result);
21832198
bool exact = sp_stdout_file_equals(fx.out, input, sizeof(input));
21842199
bool original_kept = sp_stdout_file_equals(fx.other, input, sizeof(input));
2185-
bool alternate_kept = prepared &&
2186-
sp_stdout_file_equals(alternate, alternate_bytes,
2187-
sizeof(alternate_bytes) - 1);
2200+
bool alternate_kept =
2201+
prepared && sp_stdout_file_equals(alternate, alternate_bytes, sizeof(alternate_bytes) - 1);
21882202
int cleanup = safe ? th_rmtree(fx.root) : -1;
21892203
ASSERT_EQ(cleanup, 0);
21902204
ASSERT_TRUE(prepared);
@@ -2202,8 +2216,8 @@ TEST(subprocess_stdin_rejects_invalid_paths_before_outputs) {
22022216
sp_stdout_fixture_t fx;
22032217
ASSERT_TRUE(sp_stdout_fixture_open(&fx));
22042218
char missing[SP_STDOUT_PATH];
2205-
bool prepared = sp_stdout_path(missing, fx.root, "missing-input") &&
2206-
sp_stdin_write(fx.other, "", 0);
2219+
bool prepared =
2220+
sp_stdout_path(missing, fx.root, "missing-input") && sp_stdin_write(fx.other, "", 0);
22072221
const char *paths[] = {"", missing, fx.root};
22082222
sp_stdin_rejection_t checks[3] = {{0}};
22092223
bool safe = true;
@@ -2274,8 +2288,15 @@ TEST(subprocess_stdin_posix_fifo_and_symlink_reject_without_blocking_runner) {
22742288
bool safe = true;
22752289
for (int i = 0; kinds && safe && i < 2; i++) {
22762290
(void)cbm_unlink(fx.gate);
2277-
const char *argv[] = {sp_stdout_test_binary, "__cbm_subprocess_stdin_probe", "reject",
2278-
paths[i], fx.out, fx.log, fx.gate, fx.other, NULL};
2291+
const char *argv[] = {sp_stdout_test_binary,
2292+
"__cbm_subprocess_stdin_probe",
2293+
"reject",
2294+
paths[i],
2295+
fx.out,
2296+
fx.log,
2297+
fx.gate,
2298+
fx.other,
2299+
NULL};
22792300
/* The probe can itself block inside spawn on a broken blocking FIFO
22802301
* open. Only that isolated process is then killed by this safety guard.
22812302
* No elapsed-time threshold is used as evidence of correct rejection. */
@@ -2323,6 +2344,7 @@ SUITE(subprocess) {
23232344
RUN_TEST(subprocess_outcome_str);
23242345
RUN_TEST(subprocess_run_clean);
23252346
RUN_TEST(subprocess_run_exit_nonzero);
2347+
RUN_TEST(subprocess_run_child_exited_before_group_check);
23262348
RUN_TEST(subprocess_run_resolves_literal_binary_name_from_path);
23272349
RUN_TEST(subprocess_run_crash_is_crash);
23282350
RUN_TEST(subprocess_run_hang_is_hang);

0 commit comments

Comments
 (0)