Repository navigation
serialize session socket claims - #19453
Conversation
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Good catch on the pid file. 9dccbcc adds an The inline threads are answered in place; the two non-blocking suggestions are filed as #19537 and rstudio/rstudio-pro#13571. |
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>.lockfile 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:
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 (
enableHeaderHandlerswas unavailable); full UI packaging was not validated.