Skip to content

core: reap child processes that exit before tracking - #18630

Merged
kevinushey merged 2 commits into
mainfrom
codex/fix-rsession-restart-flake
Aug 25, 2026
Merged

kevinushey merged 2 commits into
mainfrom
codex/fix-rsession-restart-flake

Conversation

@kevinushey

@kevinushey kevinushey commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • close a SIGCHLD registration race in ChildProcessTracker::addProcess by probing the child with waitpid(WNOHANG) immediately after registration
  • add a regression test that starts with an already-exited, unreaped child and verifies that registration reaps it and delivers its raw exit status
  • keep the Playwright restart timeout at 60 seconds

Root 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 launchChildProcess and ChildProcessTracker::addProcess:

  1. the child exits very early during the restart-boundary launch
  2. rserver's SIGCHLD waiter consumes the signal while the tracker does not contain the pid yet
  3. addProcess records the pid after the only notification has passed
  4. the unreaped child remains a zombie, for which kill(pid, 0) still succeeds
  5. the live-process guard added for rserver can double-launch a session during restart, leaking an orphaned rsession #18572 therefore retains the pending launch until its one-minute expiry

That 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)

@kevinushey
kevinushey force-pushed the codex/fix-rsession-restart-flake branch from d9741c7 to e862e11 Compare August 24, 2026 20:07
@kevinushey kevinushey changed the title e2e: allow pending-launch recovery during session restart core: reap child processes that exit before tracking Aug 24, 2026
@kevinushey
kevinushey requested a review from gtritchie August 24, 2026 20:10
// 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! A couple minor findings posted, neither of them are blocking.

@gtritchie gtritchie added this to the 2026.09 (Autumn Hawkbit) milestone Aug 24, 2026
@github-actions

github-actions Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
2425 0 152 4
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 603 0 31 0
✅ desktop-macos 608 0 26 1
✅ desktop-windows 607 0 31 2
✅ server-linux 579 0 44 1
✅ setup 26 0 10 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit 9635bd712e78461065d05c46e5435baf295ff2d3 - Run #692

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.
@kevinushey
kevinushey merged commit 2188574 into main Aug 25, 2026
29 checks passed
@kevinushey
kevinushey deleted the codex/fix-rsession-restart-flake branch August 25, 2026 14:01
@gtritchie gtritchie mentioned this pull request Aug 31, 2026
3 of 4 tasks

This branch was previously deployed

1 inactive deployment
e2e-playwright — 9635bd71 Deployed Aug 25, 2026 by kevinushey via windows / e2e-desktop-windows (5) #692
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants