Skip to content

Don't relaunch slow-starting sessions while their process is alive - #18961

Merged
gtritchie merged 9 commits into
mainfrom
bugfix/slow-sessions-trigger-lock-issue
Oct 2, 2026
Merged

gtritchie merged 9 commits into
mainfrom
bugfix/slow-sessions-trigger-lock-issue

Conversation

@ssinnott

@ssinnott ssinnott commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

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

  • The change lives in ServerSessionManager.cpp. Sessions we can't identify a process for (custom launchers) keep the one-minute rule.
  • Past three minutes a still-running session is relaunched anyway, so a hung start is still replaced, but it can leave two processes behind.
  • The session manager now takes its clock and "is this process running?" check as settings, so tests can control them. Production uses the real ones.
  • Also fixes a race where server option lookups from simultaneous launches could write to shared state (ThreadSanitizer found it).

Automated Tests

New SessionManager unit 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 32 SessionManager tests 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

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

Commits

  • db06620402 - Don't relaunch a slow-starting session while its process is alive
  • 400dacbbd6 - Don't insert into overlay options on lookup
  • 111ad4fd75 - Drive SessionManager tests with a fake clock and liveness check
  • 39fddca3dd - Test SessionManager with real child processes

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.
@ssinnott
ssinnott force-pushed the bugfix/slow-sessions-trigger-lock-issue branch from 53dcdb1 to 39fddca Compare September 26, 2026 00:20
@github-actions

github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
3357 0 210 3
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 847 0 39 1
✅ desktop-macos 845 0 43 0
✅ desktop-windows 838 0 54 1
✅ server-linux 797 0 44 1
✅ setup 28 0 20 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 9177e52cd149a4ae977273308c9aa2b70243d4ae - Run #1306

@gtritchie gtritchie added this to the 2026.10 (Blue Mistflower) milestone Sep 28, 2026
The #18941 launchTime merged in from main used the real clock, so pending launches never aged under the tests' fake clock.
@gtritchie

Copy link
Copy Markdown
Member

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
@ssinnott
ssinnott requested a review from gtritchie October 1, 2026 18:24

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

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:

  1. 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 until stalePendingLaunchAge (3 min), not the 1-minute launchWindow. The neighbouring comment at line 538 was updated; this one wasn't.

  2. notePendingLaunchPid doesn'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 matching launchTime the way endLaunching does. 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 passing launchTime through and matching on it, as endLaunching does, would close it.

return Success();
}
else if (pos->second.pid != -1 &&
(pos->second.launchTime + config_.stalePendingLaunchAge) > now &&

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.

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

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.

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;

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.

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)

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.

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) =>

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.

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.

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

LGTM!

@gtritchie
gtritchie merged commit 77b0b92 into main Oct 2, 2026
35 checks passed
@gtritchie
gtritchie deleted the bugfix/slow-sessions-trigger-lock-issue branch October 2, 2026 15:59
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.

2 participants