Repository navigation
Description
Symptom
An R session restart (or any session relaunch: project switch, suspend/resume) can leave behind an orphaned rsession process that runs forever. rserver launches two replacement sessions ~1s apart; both fully initialize and bind the same session stream socket; the proxy connects to whichever bound last, and the loser is never contacted again -- and never told to exit.
The orphan is invisible but fully alive: it holds the project's source-database lock (so the visible replacement reports "The previous R session terminated abnormally" and the client shows the modal R Session Error dialog), holds the Posit Assistant install in-use lock (so chat_start_backend in the visible session fails forever with a spurious "A Posit Assistant update is in progress" -- see #18571), auto-resumes its chat backend via the suspend/resume state, and keeps reacting to shared user-prefs changes (starting/stopping assistant agents) for as long as it lives.
Observed on the e2e server shard in run 31839732347: the orphan (pid 9716) was launched at 21:29:58, replaced at 21:29:59, and was still alive and spawning assistant agents at 21:43 -- 13 minutes later. Likely also a contributor to the #18394 wedge family.
Root cause
From the rserver debug log of that run:
21:29:57.929 POST /rpc/suspend_for_restart -> 200 (old session acks and begins forced suspend)
21:29:58.161 get_events -> error: system error 2 (No such file or directory) (old session's socket gone)
21:29:58.163 /rpc/ping connect fails -> recovery -> "Launching session for user: ..."
21:29:58.165 Launched session process ... pid: 9716
21:29:58.200 -- rpc error: other error for: /rpc/get_environment_state ... error: asio.misc error 2 (End of file)
21:29:59.171 "Launching RStudio session for: ..." (second launch!)
21:29:59.172 Launched session process ... pid: 9722
21:30:02.346 RStudio session() started and connection made in 3.175s (to 9722; 9716 orphaned)
The double launch happens because the pending-launch dedupe in SessionManager::launchSession was defeated:
/rpc/ping's connect fails, the retry profile's recovery launches pid 9716, and apendingLaunches_entry is recorded (ServerSessionManager.cpp)./rpc/get_environment_state-- accepted by the old session just before it stopped its listener -- dies with EOF as the old session exits. Its error handlerhandleRpcError(ServerSessionProxy.cpp:649;handleContentErrorat :562 has the same call) unconditionally callssessionManager().removePendingLaunch(context), erasing the 37 ms-old pending entry for 9716 -- a live, booting process./rpc/pingkeeps retrying its connect every 25 ms (failing while 9716 boots -- session startup takes ~3 s); its per-request launch throttle allows another recovery pass after 1 s, which now finds no pending launch and spawns 9722.- Both sessions initialize. 9716 binds the stream socket first (and wins the project source-database lock, and resumes the chat backend); 9722 re-binds the same socket path over it. The proxy connects to 9722. 9716 is unreachable (its listener holds an unlinked inode), is never sent a request or a quit, and leaks.
An in-flight RPC dying with EOF at the restart boundary is near-deterministic (the exiting session resets established connections when its listener stops), so this race window is hit by exactly the workload the e2e suite runs: session restarts with active background traffic.
Fix
removePendingLaunch on the request-error paths must not erase a pending launch whose recorded process is still alive: the failure of some request for the context says nothing about the health of a separately launched, still-booting session. Guard the success == false removal with pid != -1 && core::system::isProcessRunning(pid). If the launched process never becomes reachable, the existing safety nets still clear the entry: the SIGCHLD process tracker removes it when the pid dies (#18208), and the one-minute relaunch window in launchSession expires it otherwise. Custom session launchers (job launcher) never record a pid, so their behavior is unchanged.
Follow-ups (not this fix):
- The success path (
handleProxyResponse) has a much narrower variant of the same race (a last-gasp response from the exiting predecessor between listener-stop and process-exit would also clear a fresh pending launch); in practice the exiting session resets in-flight connections, so errors -- not successes -- are what land in the window. - An rsession could self-detect having been supplanted (periodically stat its stream-socket path and exit when the inode no longer matches its listener), which would clean up any orphan regardless of cause.
#18571 covers making the assistant install-lock owner id per-process so that even an orphaned predecessor cannot permanently block its replacement's chat backend.
Activity
fix rserver double session launch that leaks an orphaned rsession (#1…
treat EPERM as alive in the pending-launch liveness probe (#18574)
Don't relaunch a slow-starting session while its process is alive
don't let a duplicate session launch take over a live session's socket (
Don't relaunch slow-starting sessions while their process is alive (#…