Repository navigation
Don't relaunch slow-starting sessions while their process is alive - #18961
Conversation
Port of rstudio/rstudio-pro#12967. An aged pending launch whose recorded pid is still running is no longer relaunched until the stale pending launch age; a duplicate would rebind the same session socket and fail on the first session's source database locks. Addresses #18572.
getOverlayOption used operator[], so the first lookups of an unset key (e.g. ssl-enabled, read on every session launch) inserted into the map from concurrent request threads. ThreadSanitizer flagged the race in the concurrent SessionManager launch tests.
Take the pending-launch timeouts, clock, and process liveness check as a Config, so tests build their own SessionManager instead of sharing the global singleton; construction stays private, with the test fixture as a friend. Cover the launch window and stale-age edges, request errors against live, dead, and other-user processes, relaunch races, and concurrent launches and removals.
Launch /bin/sh stubs through the default launcher to cover child exits, exec failures, zombies, a replacement outliving a stale launch, and the arguments and environment a session receives. Also summarize each SessionManager test in plain terms.
53dcdb1 to
39fddca
Compare
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 |
The #18941 launchTime merged in from main used the real clock, so pending launches never aged under the tests' fake clock.
|
FYI, there's a flakey C++ unit test that's been failing some PRs (occasionally), including, I think, an earlier run of this one. Have a fix incoming. |
…trigger-lock-issue # Conflicts: # src/cpp/server/ServerSessionManagerTests.cpp # src/cpp/server/session/ServerSessionManager.cpp
gtritchie
left a comment
There was a problem hiding this comment.
A few notes from a review pass. Nothing here is a hard blocker; the first inline comment (the 3-minute cap) is a design question. Two items are on lines outside the diff, so they're here:
-
Stale comment in
resolvePendingLaunch(ServerSessionManager.cpp:522): "a wrapper that runs rsession as a child never matches, so its entry stays until the wrapper exits or the launch window lapses." With this change a live wrapper's pid holds the entry untilstalePendingLaunchAge(3 min), not the 1-minutelaunchWindow. The neighbouring comment at line 538 was updated; this one wasn't. -
notePendingLaunchPiddoesn't check which launch it belongs to (ServerSessionManager.cpp:680). This is pre-existing, but the new liveness check now relies on the recorded pid. It writes the pid onto whatever entry exists for the context, without matchinglaunchTimethe wayendLaunchingdoes. If launch A spends more than a minute in the launch step (pid still -1), launch B replaces A's entry; if A then records its pid after B does, B's entry carries A's pid. B's exit then doesn't clear the entry, and A's exit clears B's entry instead. Rare, but passinglaunchTimethrough and matching on it, asendLaunchingdoes, would close it.
| return Success(); | ||
| } | ||
| else if (pos->second.pid != -1 && | ||
| (pos->second.launchTime + config_.stalePendingLaunchAge) > now && |
There was a problem hiding this comment.
Past stalePendingLaunchAge this falls through to the erase-and-relaunch branch even when isProcessRunning(pid) is still true, so a session that takes longer than 3 minutes to start (e.g. a large workspace restore on slow storage) still gets a second process started on top of it: the same socket/lock fight this PR fixes, just later. If the replacement loses the bind race it fails; if it wins, the old process is orphaned holding the project locks (#18572). cleanStalePendingLaunches does the same to other contexts' live-pid entries at 3 minutes.
I see the PR description calls this tradeoff out. Would it be better to keep live-pid entries until the exit tracker clears them, or to terminate the recorded pid before relaunching, so replacing a hung start doesn't leave two processes behind?
| // if the launch is less than one minute old then return success | ||
| if ( (pos->second.launchTime + boost::posix_time::minutes(1)) | ||
| > microsec_clock::universal_time() ) | ||
| // if the launch is still within its window, or its process is still |
There was a problem hiding this comment.
Minor: this says the launch is honored while "its process is still alive", but the second branch caps that at stalePendingLaunchAge. Worth mentioning the cap so this doesn't read as "never relaunch a live process".
| SessionManager(); | ||
| explicit SessionManager(const Config& config); | ||
| friend SessionManager& sessionManager(); | ||
| friend class tests::SessionManagerTest; |
There was a problem hiding this comment.
Minor: as far as I can tell this is the first test-class friend in a production header under src/cpp. It ties the tests to pendingLaunches_ and launchesMutex_, so renaming either breaks them. A SessionManager(const Config&) constructor the tests can reach, plus a small read-only accessor for a context's pending pid, would cover what the tests need without the friend.
| SessionManager::SessionManager(const Config& config) | ||
| : config_(config) | ||
| { | ||
| if (!config_.now) |
There was a problem hiding this comment.
Nit: default member initializers on Config (now = ...universal_time, isProcessRunning = ...) would show the production defaults in the header and drop these fallbacks. It would need the core::system include in the header for isProcessRunning, so take it or leave it.
| // not ready to close, revert close stage and take care of business | ||
| this.closeStage = 'CloseStageOpen'; | ||
| this.executeJavaScript('window.rstudioCloseSourceWindow()').catch((error: unknown) => logger().logError(error)); | ||
| this.executeJavaScript('window.rstudioCloseSourceWindow()').catch((error: unknown) => |
There was a problem hiding this comment.
This reflow came in through the merge commit 4e8688a rather than one of the PR's own commits. The line is 124 chars on main, over the 120 printWidth, so the fix is right, but it's unrelated to the session-launch change. Could it go in its own PR?
- Replace the tests' friend access with a public Config constructor and read-only pendingLaunchPid/pendingLaunchCount accessors. - Move the production clock and liveness defaults onto Config. - Note the stale-age cap in the launchSession liveness comment, and fix the resolvePendingLaunch comment that still cited the launch window for wrapper entries.
Intent
A session that takes over a minute to start was launched a second time on top of itself, and the duplicate failed fighting the first over its socket and project locks.
Similar issues was fixed in #18572; but there's another variant over here: https://github.com/rstudio/rstudio-pro/issues/12960
Approach
ServerSessionManager.cpp. Sessions we can't identify a process for (custom launchers) keep the one-minute rule.Automated Tests
New
SessionManagerunit tests use a fake clock and fake process checks to cover the one- and three-minute cutoffs, failed requests while a session is starting, old sessions exiting after a replacement starts, and many simultaneous launches. Tests that start real child processes cover crashes, a program that can't run, exited-but-not-yet-cleaned-up processes, and the arguments passed to a session. All 32SessionManagertests pass locally.QA Notes
This change is split between the pro + rstudio side. Expanded coverage to also have better hooks on that side.
Documentation
No documentation changes; this is internal session-launch behavior.
Checklist
NEWS.mdCommits