Repository navigation
Description
Symptom
On RStudio Server, a duplicate launch of a user's session locks the user out of it. Every page load shows
R Session Aborted
R encountered a fatal error.: ERROR system error 77 (No locks available) [description: RStudio cannot use this session's recovery data because it is locked. Another RStudio session may be using it. Close that session or wait a moment, then try again., source-directory: ~/scratch/abc/.Rproj.user/C50988BB/sources/session-1864d529]; OCCURRED AT ... sessionDirInUseError ... SessionSourceDatabaseSupervisor.cpp:336
and Start New Session fails the same way, until the original rsession process is killed by hand. That process is alive and healthy (idle at the console prompt), but nothing can reach it any more.
Seen on a dev server (npm run rserver-dev, macOS) built from main as of 2928c79, plus a branch that doesn't touch session launching, sockets, or the source database.
What happened
10:10:57 rsession 90197 launched for the session (the original; project ~/scratch/abc)
17:11:00Z [rsession-kevin] ERROR system error 77 (No locks available) ... R SUICIDE (a second rsession for the same session)
17:11:30Z [rsession-kevin] ERROR system error 77 (No locks available) ... R SUICIDE (Start New Session)
17:11:33Z [rsession-kevin] ERROR system error 77 (No locks available) ... R SUICIDE (and again)
Afterwards:
lsofshowed 90197 holding.../sources/session-1864d529/lock_file, and still holding its listener on$TMPDIR/rsd-bO5V8b/rstudio-rsession/kevin-d, but that path no longer existed (therstudio-rsessiondirectory was empty).- A stack sample of 90197 showed it idle in
waitForMethodat the console prompt. kill 90197ended the lockout: the next page load started a working session.
What prompted the second launch three seconds after the first is not known (the user was trying Restart R and browser refreshes around then). Whatever the trigger, the outcome below is new.
Root cause
Since #18853, a session that finds its own source database locked by another process exits (attachToSourceDatabase -> acquireSessionDirLock -> sessionDirInUseError). A duplicate rsession for the same session gets that far only after it has taken over the session's stream socket, and it removes the socket on the way out:
- The duplicate starts its HTTP listener before R initialization (
SessionMain.cpp,startHttpConnectionListenerWithTimeout).LocalStreamAsyncServer::initfirst callsremoveLocalStream(), unlinking the live session's socket path, then binds its own. - During R initialization it attaches the source database, finds the session directory locked by the live session, and aborts.
- On exit,
~LocalStreamAsyncServercallsremoveLocalStream()again, deleting the path it had bound.
The live session is left listening on an unlinked inode. The next request can't connect, so the server launches another session, which repeats steps 1-3. Before #18853 the duplicate would have run to completion and the older process leaked as an unreachable orphan (#18572). Now the duplicate dies instead, and the user can't get back in at all.
Possible fixes
- Don't take over a live session's socket: before
removeLocalStream()ininit, try connecting to the existing socket, and if a process answers, exit without touching the path (the client is already served by that session). Alternatively, acquire the source-database lock (or another per-session ownership check) before starting the listener, so a session that can't own the session never binds. - Only remove the socket path on exit if it still refers to the socket this process bound (compare the inode recorded at bind time).
- The follow-ups listed in rserver can double-launch a session during restart, leaking an orphaned rsession #18572 still apply: the success-path variant of the pending-launch race, and having an rsession notice it has been supplanted (its socket path no longer matches its listener) and exit.
Related: #18572 (duplicate launch leaking an orphan), #18853 (made source-database lock contention fatal).
Activity
claim the session socket first, bind before probing, keep launches th…
fix socket tests on linux, keep the claimed socket from children, rel…
don't let a duplicate session launch take over a live session's socket (
Stamp pending launches with the injected clock
Don't relaunch slow-starting sessions while their process is alive (#…