Skip to content

rserver can double-launch a session during restart, leaking an orphaned rsession #18572

Description

@kevinushey

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:

  1. /rpc/ping's connect fails, the retry profile's recovery launches pid 9716, and a pendingLaunches_ entry is recorded (ServerSessionManager.cpp).
  2. /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 handler handleRpcError (ServerSessionProxy.cpp:649; handleContentError at :562 has the same call) unconditionally calls sessionManager().removePendingLaunch(context), erasing the 37 ms-old pending entry for 9716 -- a live, booting process.
  3. /rpc/ping keeps 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.
  4. 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.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions