@@ -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. */
18451863static 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
18511868static 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
18621879static 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