Skip to content

serialize session socket claims - #19453

Merged
kevinushey merged 6 commits into
mainfrom
bugfix/18962-stale-socket-claim
Nov 9, 2026
Merged

kevinushey merged 6 commits into
mainfrom
bugfix/18962-stale-socket-claim

Conversation

@kevinushey

@kevinushey kevinushey commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Concurrent launches can both decide a session socket is stale, unlink each other's newly bound sockets, and leave a running session unreachable. The same race exists between the first bind and listen on an initially missing path.

Closes #18962.

Hold an exclusive flock on a persistent <stream>.lock file for the entire claim, from before the first bind through identity capture, listen, and permission setup. Closing the descriptor releases the lock on every return; the file stays in place so waiting claimants keep using the same inode. The descriptor is close-on-exec, and symlink, special-file, and foreign-owner lock paths are rejected. Preserve the session user's lock-file ownership during rserver's startup ownership pass.

Validation:

  • Both new concurrent-claim regression tests failed before the fix, with two successful claimants and mismatched socket identities, and pass with it.
  • macOS core suite: 814 passed, 12 existing environment-dependent skips; 3 tests remain disabled.
  • macOS session listener lifecycle suite: all 7 passed.
  • Server startup ownership regression: passed; rserver and rserver-tests built successfully.
  • Ubuntu 22.04 arm64 socket suite: all 16 passed with GCC 11.4.
  • Snyk Code scan of the PR's changed files: passed with zero findings. The lock-file test uses a standard RAII file handle so Snyk can recognize its cleanup.
  • Roborev Refine: passed the full-branch re-review after fixing one MEDIUM finding about lock-file ownership on server restart. No LOW findings ignored.

The lock files intentionally remain after a session stops. No browser/manual session workflow was exercised; validation uses real Unix sockets, concurrent claims, a forked process, and the session listener tests. Linux C++ tests used development mode after the package configuration hit an unrelated GWT compile error in unchanged frontend code (enableHeaderHandlers was unavailable); full UI packaging was not validated.

@kevinushey
kevinushey requested a review from gtritchie November 5, 2026 20:50
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
5261 0 251 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1320 0 45 0
✅ desktop-macos 1315 0 51 0
✅ desktop-windows 1317 0 54 0
✅ server-linux 1257 0 53 1
✅ setup 48 0 32 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 96646be038f2af3c46bd00b7f37a983c57a59c17 - Run #2125

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

The locking approach looks right to me, and the concurrent-claim tests are good. A few comments are inline; none is blocking.

One more, outside the diff: SessionLocalStreamHttpConnectionListener::initializeAcceptor writes the pid file after claimLocalStream has released the lock. A losing duplicate launch can read <stream>.pid before the winner writes it. SessionMain then logs a missing pid, or a stale one from an earlier crashed session, as "already running". This predates the PR and only affects that log line. I'm mentioning it in case writing the pid file under the lock is easy to fit in.

struct stat lockInfo;
if (::fstat(lockFd, &lockInfo) == -1)
return systemError(errno, ERROR_LOCATION);
if (!S_ISREG(lockInfo.st_mode) || lockInfo.st_uid != ::geteuid())

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.

When the lock file is rejected here (not a regular file, or owned by another user), and when fstat fails on the line above, the returned error has no lock-file property. The open and flock failures do add it.

One way to hit this: upgrade, downgrade, then restart the older rserver. Its ownership pass exempts only -d, -d.pid, and -d.ctx, so it chowns user-d.lock to the server user. After re-upgrading, every launch of that user's session fails, and the log only says "Permission denied". The fix is just deleting the file (the rstudio-rsession directory has no sticky bit), but nothing in the log says which file.

Could these two returns add error.addProperty("lock-file", lockPath) as well? A description saying the lock file has the wrong owner or type, and can be removed, would help too.

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.

Done in 5c3902d: both returns now carry lock-file, and the rejection adds a description saying the file is not a regular file owned by the user and can be removed. One note on the downgrade scenario: the chowned lock is 0600, so that case fails in open() with EACCES, which already named the file. The description covers what reaches the ownership check (a foreign but accessible file, a FIFO, a symlink).

inline FilePath monitorSocketPath() { return serverTmpDir().completeChildPath(kMonitorSocket); }
inline FilePath sessionTmpDir() { return options().serverDataDir().completeChildPath(kSessionTmpDir); }

inline bool shouldChownServerDataFile(int depth, const FilePath& file)

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 filter now relies on the lock file sitting exactly where the socket does, at one of the two hard-coded depth/suffix combinations. Before this PR, a socket chowned by mistake was harmless because it's mode 0666. A 0600 lock chowned by mistake makes open() fail, so any stream path this filter doesn't anticipate now becomes a session that can't start after an rserver restart.

I couldn't find a code path in this repo that puts a stream at another depth, so this isn't a live bug. A more general rule would remove the need for each new session file to get its own suffix here, though. For example: never give a file that a non-root user already owns to the server user. Worth considering, not blocking.

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.

Agreed that an owner-based rule would be more robust, and that nothing in this repo puts a stream elsewhere today. I would rather take it as a follow-up: skipping every non-root-owned file also changes what the restart pass does after server-user is changed (rserver's own files owned by the previous server user would stay that way), so it wants its own look. Filed as #19537.

{
// Serialize the entire claim, including the first bind: a socket that
// has been bound but is not listening yet also looks stale to a probe.
// Keep this file after closing it. Unlinking it would let a new claimant

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.

Since these are never unlinked, every distinct stream path leaves a .lock behind. Open source has one user-d per user, so that's fine. Multi-session stream paths are per scope (user-ds/<project>/<scope-id>), though, so each session scope adds an empty file that nothing in this repo cleans up. With the default /var/run/rstudio-server on tmpfs, reboots bound the growth. Mentioning it in case the multi-session side should reap them alongside the socket directories.

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.

Right. The scope paths are created and reaped on the pro side, and this code does not know the scope lifecycle, so I would have pro's reaping remove <scope>.lock beside the socket. Filed as rstudio/rstudio-pro#13571.

@kevinushey

Copy link
Copy Markdown
Collaborator Author

Good catch on the pid file. 9dccbcc adds an onClaimed callback to claimLocalStream that runs after listen while the lock is still held, and the listener writes its pid file there, so a losing duplicate always reads the winner's pid. Two tests cover the callback running under the lock and its error propagating.

The inline threads are answered in place; the two non-blocking suggestions are filed as #19537 and rstudio/rstudio-pro#13571.

@kevinushey
kevinushey requested a review from gtritchie November 9, 2026 20:11

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

Thanks -- the lock-file errors and the pid file write under the claim lock look good. Fine with the other two as follow-ups in #19537 and rstudio/rstudio-pro#13571.

@kevinushey
kevinushey merged commit f24e397 into main Nov 9, 2026
1 of 7 checks passed
@kevinushey
kevinushey deleted the bugfix/18962-stale-socket-claim branch November 9, 2026 20:36
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.

Two session launches racing for a stale socket can both claim it

2 participants