Skip to content

attribute session request outcomes to the process that produced them - #18989

Merged
kevinushey merged 7 commits into
mainfrom
bugfix/18963-pending-launch-attribution
Sep 29, 2026
Merged

kevinushey merged 7 commits into
mainfrom
bugfix/18963-pending-launch-attribution

Conversation

@kevinushey

@kevinushey kevinushey commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Intent

Fixes #18963.

rserver keeps one pending-launch entry per session context so a session is not launched twice, and clears it when a proxied request for the context succeeds or fails. It could not tell which process produced that outcome. During a restart, the session being replaced can still answer or fail a request after the replacement was spawned, and that outcome erased the replacement's entry; the next request that found the session unavailable launched it again. #18572 and #18944 each closed one window; the three gaps listed in the issue remained and all reproduce as unit tests against main.

Approach

  • LocalStreamAsyncClient records the peer pid of each connection it makes (SO_PEERCRED on Linux, LOCAL_PEERPID on macOS, via a new socketPeerPid() next to socketPeerIdentity()), exposes it, and stamps it on errors as a peer-pid property so the error handlers, which never see the client, can read it.
  • SessionManager::removePendingLaunch() and removePendingSessionLaunch() take the peer pid. Both now share one policy: an entry still marked as launching is kept (as before); an outcome attributed to a process other than the launched one is kept; an outcome that cannot be attributed (no connection made, or a session reached over TCP) falls back to the existing liveness guard from rserver can double-launch a session during restart, leaking an orphaned rsession #18572; anything else ends the launch. The by-session-id variant used to erase unconditionally, ignoring both guards; it is the variant Workbench's launcher-session RPC forwarding calls, so those callers pick up the guards through the merge.
  • launchSession() clears the entry and its launching flag when the launch function throws, instead of leaving the context suppressed until the pending window expires.

Sessions reached over TCP (Workbench launcher sessions) still cannot attribute outcomes, since there are no peer credentials on a TCP socket. They keep the fallback guards; a generation stamp echoed by the session would close that gap and is left for a pro follow-up.

Automated Tests

  • ServerSessionManagerTests.cpp: six new cases covering an outcome from another process (success and error) keeping the entry, an error from the launched process itself clearing it, a launch without a recorded pid ignoring the peer pid, the by-session-id variant applying the same guards both after and during the launch, and a throwing launch function clearing the entry. Each of the three gaps fails on main.
  • LocalStreamAsyncClientTests.cpp (new): a real local-stream server in-process verifies the client records the peer pid on connect, stamps it on a hang-up error, and reports none when nothing was listening.
  • PosixSystemTests.cpp: socketPeerPid() on a socketpair and on a non-socket.
  • ServerSessionProxyTests.cpp: two cases run the rpc and content error handlers against a pending launch, verifying that the peer pid stamped on an error reaches the session manager. The response handler, which reads the pid from the client instead, is not covered.

QA Notes

On RStudio Server, restart R repeatedly (Session > Restart R) and restart the session from the session menu; the session should come back each time with exactly one rsession process for the user. The rserver log at debug level reports "came from process N, not the launched one" when the guard fires.

Documentation

None.

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

rserver cleared a pending session launch on any outcome for the context,
so a response or error from the session a restart was replacing could
erase the replacement's entry and let the next request launch it again.

Record the peer pid of each local stream connection and only let an
outcome from the launched process end its launch; apply the same guards
to the by-session-id removal path; and clear the entry when the launch
function throws.

Addresses #18963
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
3336 0 191 3
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 838 0 39 0
✅ desktop-macos 844 0 34 0
✅ desktop-windows 837 0 44 2
✅ server-linux 787 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 4c8e51fffd69492c89bb201b9c1bb493d7693bfd - Run #1285

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

Six findings, inline. None is blocking; the first is the only correctness concern and it depends on an off-label rsession-path setup. The rest are design, cleanup, and test-coverage items.

Comment thread src/cpp/core/system/PosixUser.cpp Resolved
The outcomes are mutually exclusive, so one enum plus one pid replaces the flag-and-sentinel fields, and a switch does the logging. Addresses #18963.
socketPeerIdentity() and socketPeerPid() read the same ucred on Linux. Addresses #18963.
The server blocked in accept() and its destructor joined the thread, so a client that failed before connecting hung the test binary instead of failing the test. Accept asynchronously and stop the io_context before joining. Addresses #18963.
Covers the step between the client stamping the peer pid on an error and the session manager acting on it. Addresses #18963.
Matches the other server tests, and keeps the test classes out of the global namespace. Addresses #18963.

@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, all six addressed. The launch-token follow-up is tracked in rstudio/rstudio-pro#13000.

@kevinushey
kevinushey merged commit 67475f9 into main Sep 29, 2026
35 checks passed
@kevinushey
kevinushey deleted the bugfix/18963-pending-launch-attribution branch September 29, 2026 21:14
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.

rserver can't tell which session process a request outcome came from, so a predecessor's response can clear a new launch's pending entry

2 participants