Skip to content

speed up session suspend/shutdown after interrupt - #18091

Merged
kevinushey merged 3 commits into
mainfrom
bugfix/faster-session-suspend
Jun 30, 2026
Merged

kevinushey merged 3 commits into
mainfrom
bugfix/faster-session-suspend

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Summary

When rserver shuts down (e.g. Ctrl-C or rstudio-server stop) it sends SIGTERM to each rsession, which in server mode triggers a forced suspend. Investigating why that took several seconds even for an idle, empty session turned up two avoidable delays, both fixed here.

1. Client event service: 4s "last chance" wait that can't succeed

ClientEventService::stop() gave the browser a kLastChanceWaitSeconds (4s) window to retrieve any remaining events. During a server shutdown the HTTP front door is already closed (rserver stops serving before signaling children), so no client can connect and the wait always elapsed in full.

  • stop() now takes flushPendingEvents (default true). rCleanup passes !suspend::suspendedForcibly(), so a forced suspend skips the wait entirely.
  • The run loop additionally gates the wait on recent client activity (eventsConnectionQueue().lastConnectionTime()), so an idle/closed client on a non-forced (e.g. timeout) suspend doesn't pay the 4s either. A genuinely connected client still gets its events (delivered on the held long-poll connection, and via a fast reconnect).

2. saveOptions: serializing RStudio-managed function options

.rs.saveOptions serialized a few RStudio-managed, function-valued options whose closures capture the large internal .rs tools environment, costing ~0.7-0.8s each even with an empty workspace: connectionObserver, terminal.manager, reticulate.repl.*, and RStudio's own error handler. All of these are re-established on every session start, so they are now dropped before serialization.

error is only dropped when it is a handler RStudio installed (identified by the rstudioErrorHandler attribute on the function embedded in the option's call object); user-supplied error handlers are preserved.

Impact

A forced suspend on an otherwise-empty server session drops from ~4-5s to well under 1s (measured via temporary per-phase instrumentation, since removed).

Notes

  • Found during internal investigation; no associated GitHub issue.
  • Verified by reproducing a forced suspend (Ctrl-C on rserver) and a quit on a connected session, confirming the event-flush still occurs for connected clients and that a user-set options(error=) survives suspend/resume.

When rserver shuts down (e.g. on Ctrl-C or service stop) it sends SIGTERM
to each rsession, which in server mode triggers a forced suspend. Two
parts of that path added several seconds of avoidable delay:

- The client event service gave the browser a 4-second "last chance" to
  retrieve final events on stop. During a server shutdown the HTTP front
  door is already closed, so that wait always timed out in full. stop()
  now takes a flushPendingEvents flag (false on a forced suspend), and
  the run loop also skips the wait when no client connection has been
  serviced recently -- so an unreachable client no longer costs 4s.

- Saving R options serialized a handful of RStudio-managed,
  function-valued options (connectionObserver, terminal.manager,
  reticulate.repl.*, and RStudio's own error handler) whose closures
  capture the large internal .rs tools environment, costing ~0.7-0.8s
  each. These are re-established on every session start, so .rs.saveOptions
  now drops them before serializing. The error option is only dropped when
  it is one RStudio installed (identified by the rstudioErrorHandler tag on
  the embedded function); user-supplied handlers are preserved.

Together these cut a forced suspend on an otherwise-empty session from
several seconds to well under one.
@kevinushey kevinushey added this to the 2026.07 (Pacific Dogwood) milestone Jun 29, 2026
@github-actions

github-actions Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

🍎 macOS Desktop E2E · ARM64

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
415 0 8 0
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · macos-26 ARM64 · commit 156e84619b249391b6342cd5797f3aa499fa64be · Run #29

@github-actions

github-actions Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

🐧 Linux Server E2E · Ubuntu 24

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
395 0 20 3
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · ubuntu-24.04 · commit 156e84619b249391b6342cd5797f3aa499fa64be · Run #29

@github-actions

github-actions Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

🐧 Linux Desktop E2E · Ubuntu 24

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
415 0 8 0
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · ubuntu-24.04 · commit 156e84619b249391b6342cd5797f3aa499fa64be · Run #29

@github-actions

github-actions Bot commented Jun 30, 2026 •

Copy link
Copy Markdown

🪟 Windows Desktop E2E · Server 2025 x64

✅ All tests passed!

████████████████████ 100% pass rate

✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
415 0 10 0
📥 Download Playwright report

⬇️ playwright-report.zip — extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


2 shards · Windows Server 2025 x64 · commit 156e84619b249391b6342cd5797f3aa499fa64be · Run #29

@kevinushey
kevinushey merged commit 56ee787 into main Jun 30, 2026
1 check passed
@kevinushey
kevinushey deleted the bugfix/faster-session-suspend branch June 30, 2026 16:20
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.

1 participant