Skip to content

Commit 98aff5b

Browse files
committed
fix(subprocess): retry a spawn the kernel refused for load, not for cause
`posix_spawnp` and `fork` both fail with EAGAIN when the process table or a per-user limit is momentarily full. Both paths treated that as a hard failure, so a burst of process creation turned into CBM_PROC_SPAWN_FAILED — a git probe, an LSP server, or an index worker refusing to run for a reason the user can neither see nor act on, on a machine that is merely busy. This is the production half of a flake we have been re-running for two days: `subprocess_run_clean` failed spawning `/bin/sh -c "exit 0"` on macOS runners in three separate CI runs, always under a loaded release matrix, always green on a quiet rerun. Chasing it as a test race would have been chasing the symptom — the test is right, and a user on a loaded laptop hits the same edge. EAGAIN/ENOMEM now retry three times over ~30ms; everything else keeps failing immediately, and an exhausted system still fails after the backoff rather than spinning. exec-class errors (ENOENT/EACCES/ENOEXEC/...) are untouched, so the documented "missing binary exits 127, spawn_failed means the mechanism failed" contract still holds. The fork fallback gets the same treatment: it fails for the same reason under the same pressure, and the fallback must not be less robust than the primary path. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 4b88690 commit 98aff5b

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

‎src/foundation/subprocess.c‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,26 @@ static cbm_proc_poll_t cbm_subprocess_poll_win(cbm_subprocess_t *process, cbm_pr
882882

883883
#else /* POSIX */
884884

885+
/* Transient spawn-failure retry (see the EAGAIN note in cbm_posix_spawn_apple).
886+
* Three attempts over ~30ms total: long enough to ride out a burst of process
887+
* creation, short enough that a genuinely exhausted system still fails fast. */
888+
enum { CBM_SPAWN_RETRY = 2, CBM_SPAWN_RETRY_ATTEMPTS = 3 };
889+
static const struct timespec CBM_SPAWN_RETRY_DELAY = {0, 10L * 1000L * 1000L};
890+
#define CBM_SPAWN_RETRY_DELAY_NS (&CBM_SPAWN_RETRY_DELAY)
891+
892+
/* fork() fails with EAGAIN under the same pressure posix_spawn does, and the
893+
* fallback path must not be less robust than the primary one. */
894+
static pid_t cbm_fork_with_retry(void) {
895+
for (int attempt = 0; attempt < CBM_SPAWN_RETRY_ATTEMPTS; attempt++) {
896+
pid_t pid = fork();
897+
if (pid >= 0 || (errno != EAGAIN && errno != ENOMEM)) {
898+
return pid;
899+
}
900+
(void)cbm_nanosleep(CBM_SPAWN_RETRY_DELAY_NS, NULL);
901+
}
902+
return fork();
903+
}
904+
885905
/* Used by the fork+exec child. posix_spawn performs the same reset
886906
* declaratively via SETSIGDEF + SETSIGMASK, but Apple still forks for the
887907
* exec-failure fallback below, so this stays compiled everywhere. */
@@ -1010,6 +1030,14 @@ static int cbm_posix_spawn_apple(cbm_subprocess_t *process, int input, int outpu
10101030
rc == ELOOP || rc == ENAMETOOLONG || rc == ENOTDIR)) {
10111031
return 1;
10121032
}
1033+
/* EAGAIN/ENOMEM are the kernel saying "not right now", not "never": the
1034+
* process table or a per-user limit is momentarily full. Reporting
1035+
* spawn_failed for that turns transient load into a user-visible error —
1036+
* a git or LSP probe failing on a busy laptop for no reason the user can
1037+
* see or act on. Retry briefly. Everything else stays a hard failure. */
1038+
if (configured && (rc == EAGAIN || rc == ENOMEM)) {
1039+
return CBM_SPAWN_RETRY;
1040+
}
10131041
return -1;
10141042
}
10151043
#endif
@@ -1059,13 +1087,21 @@ static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
10591087
pid_t pid = -1;
10601088
#ifdef __APPLE__
10611089
int spawn_rc = cbm_posix_spawn_apple(process, input, output, &pid);
1090+
for (int attempt = 0; spawn_rc == CBM_SPAWN_RETRY && attempt < CBM_SPAWN_RETRY_ATTEMPTS;
1091+
attempt++) {
1092+
(void)cbm_nanosleep(CBM_SPAWN_RETRY_DELAY_NS, NULL);
1093+
spawn_rc = cbm_posix_spawn_apple(process, input, output, &pid);
1094+
}
1095+
if (spawn_rc == CBM_SPAWN_RETRY) {
1096+
spawn_rc = -1; /* still exhausted after backoff: a real failure */
1097+
}
10621098
if (spawn_rc < 0) {
10631099
(void)close(input);
10641100
(void)close(output);
10651101
return -1;
10661102
}
10671103
if (spawn_rc > 0) { /* exec-class failure: reproduce the fork+exec 127 */
1068-
pid = fork();
1104+
pid = cbm_fork_with_retry();
10691105
if (pid < 0) {
10701106
(void)close(input);
10711107
(void)close(output);
@@ -1076,7 +1112,7 @@ static int cbm_subprocess_spawn_posix(cbm_subprocess_t *process) {
10761112
}
10771113
}
10781114
#else
1079-
pid = fork();
1115+
pid = cbm_fork_with_retry();
10801116
if (pid < 0) {
10811117
(void)close(input);
10821118
(void)close(output);

0 commit comments

Comments
 (0)