Repository navigation
core: reap child processes that exit before tracking - #18630
Conversation
d9741c7 to
e862e11
Compare
| // empty, no later signal will prompt us to reap the resulting zombie. | ||
| // Probe once after registration to close that race; waitpid(WNOHANG) is a | ||
| // no-op while the child is still running. | ||
| attemptToReapProcess(process); |
There was a problem hiding this comment.
The probe now races the sigwait thread for the same pid, and the loser logs a spurious error.
notifySIGCHILD (driven from ServerMain.cpp:477) snapshots activeProcesses() outside the mutex, so if a child exits right at registration time: the io thread inserts pid P here; the signal thread wakes, sees P in its snapshot and reaps it; this call then runs waitPid(P), gets -1/ECHILD, and falls into the result == -1 branch at line 107, which does LOG_ERROR(systemError(ECHILD)) with pid=P. The exit handler still fires exactly once, so the only damage is a bogus "No child processes" error in the rserver log -- in precisely the scenario this PR is meant to fix. Before this change only the single sigwait thread ever reaped, so a benign ECHILD here was impossible.
Suggest treating ECHILD as "already reaped" rather than an error (either in this probe path or in attemptToReapProcess).
There was a problem hiding this comment.
Confirmed, fixed in 9635bd7.
One extension to the analysis: it isn't only the probe that can lose the race. The waiter's activeProcesses() snapshot is taken outside the mutex, so it can predate the probe's removeProcess and lose just as easily. That rules out handling this in the probe path alone, so the carve-out went into attemptToReapProcess: ECHILD is now treated as "already reaped" and noted with DLOGF rather than LOG_ERROR. This matches the existing carve-outs in PosixChildProcess.cpp (lines 949 and 1242).
| EXPECT_FALSE(isProcessRunning(std::numeric_limits<pid_t>::max())); | ||
| } | ||
|
|
||
| TEST(PosixTests, ChildProcessTrackerReapsChildThatExitedBeforeRegistration) |
There was a problem hiding this comment.
This covers the already-exited child, but the other half of the new contract -- "waitpid(WNOHANG) is a no-op while the child is still running" -- is only asserted in a comment.
addProcess now does a waitpid on the launching thread for every session launch, so the live-child path is the common one. If waitPid ever loses WNOHANG (or gains WUNTRACED/WCONTINUED), addProcess would block the io thread mid-launch, or fire the exit handler for a merely stopped child and clear a healthy pending launch -- and this suite would still pass.
A second case that registers a child kept alive (e.g. forked child blocking on a pipe read), asserts addProcess returns promptly with exitHandled == false and the pid unreaped, then kills and reaps it, would pin that down.
There was a problem hiding this comment.
Added ChildProcessTrackerLeavesRunningChildAlone in 9635bd7. It registers a still-running child, asserts addProcess returns with exitHandled == false and waitpid(pid, nullptr, WNOHANG) == 0 (alive and unreaped), then kills it and checks the tracker still reaps it through notifySIGCHILD.
The child sleeps rather than blocking forever so that dropping WNOHANG fails an expectation instead of hanging the suite -- verified by mutating WNOHANG out of waitPid and rebuilding, which fails the test at 30s rather than hanging.
On the WUNTRACED half: that one is already guarded. A merely stopped child would return the pid from waitpid but fail the existing WIFEXITED || WIFSIGNALED check, so it logs a warning and leaves the pid registered rather than firing the exit handler and clearing a healthy pending launch.
gtritchie
left a comment
There was a problem hiding this comment.
LGTM! A couple minor findings posted, neither of them are blocking.
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
The addProcess probe means two threads can now reach attemptToReapProcess for the same pid: the launching thread's probe, and the SIGCHLD waiter, whose activeProcesses() snapshot is taken outside the mutex and so can predate the probe's removeProcess. Only one waitpid can win; the loser got ECHILD and logged a spurious "No child processes" error in precisely the scenario this branch is meant to fix. Treat ECHILD as "already reaped" and note it at debug level instead, matching the existing carve-outs in PosixChildProcess.cpp. Also add ChildProcessTrackerLeavesRunningChildAlone, which pins the other half of the contract: addProcess must neither block on nor reap a child that is still running. The child sleeps rather than blocking forever so that a regression which drops WNOHANG fails the expectation rather than hanging the suite.
Summary
ChildProcessTracker::addProcessby probing the child withwaitpid(WNOHANG)immediately after registrationRoot cause
The linux-server shard on PR #18628 did launch replacement rsession processes, pids 9805 and 10179. Neither ever created the session socket, neither produced an rsession log, and neither triggered the pending-launch exit callback. rserver consequently logged that each pid was still live until the one-minute pending-launch window expired; the next replacement then connected in 1-3 seconds.
There is a race between
launchChildProcessandChildProcessTracker::addProcess:addProcessrecords the pid after the only notification has passedkill(pid, 0)still succeedsThat accounts for the otherwise contradictory evidence: no socket or exit callback, but a pid reported as live for exactly the pending-launch window.
Probing once after insertion closes the missed-notification window. If the child still runs,
waitpid(WNOHANG)is a no-op. If it already exited, the normal exit handler clears the pending launch immediately and the buffered request can launch another session.Testing
rstudio-core-tests --gtest_filter='PosixTests.ChildProcessTrackerReapsChildThatExitedBeforeRegistration'rstudio-core-tests --gtest_filter='PosixTests.*'(31 passed)rstudio-core-tests(615 passed, 8 skipped)