Repository navigation
fix windows hang on project switch from unstopped io_context workers - #18021
Conversation
On project switch/close, the desktop session quits via exitEarly() -> ::exit(), which runs static/global destructors. The server_rpc file-scope io_context worker thread was launched detached and never stopped, so ~io_context() could run on the main thread while the worker was still inside run() -- undefined behavior that hangs on Windows (IOCP). This began affecting ordinary desktop users once the PPM vulnerability fetch (#17808) started driving async work on that io_context for any session using a Posit Package Manager repository (#17823). Retain a joinable handle for the server_rpc worker and stop+join it from exitEarly(), via a new core::thread::joinOrAbandonThread() helper that bounds the wait (timed_join) and detaches on timeout, so the exit path can never trade the teardown hang for an indefinite join hang. The helper never throws (safe to call from a destructor) and requires callers to state whether the thread needs interrupting, since io_context workers are not interruption points and must be released via io_context::stop(). The monitor io_context worker had the same latent race (stopped but never joined); fix it the same way. Migrate the other duplicated join/log/detach shutdown tails (OfflineService, ClientEventService, file monitor, ConsoleInputService, HttpConnectionListener) onto the shared helper; this also fixes a latent std::terminate in ConsoleInputService's destructor, which left its thread joinable on a join timeout. Addresses #18018.
.rs.ppm.fromRepositoryUrl matched with a fully-anchored regex ending in (?<snapshot>[^/]+)$, so a repo URL with a trailing slash (e.g. ".../cran/latest/") did not match and yielded an empty vulnerability request plan. RStudio's own CRAN-mirror UI normalizes repo URLs with a trailing slash, so users who configured a PPM repo that way silently got no vulnerability badges. Tolerate an optional trailing slash in the detection regex. Addresses #18018.
ServerMain's shutdown child-reaper thread used a bare timed_join(60s); on timeout the thread stayed joinable and ~boost::thread would std::terminate. Route it through core::thread::joinOrAbandonThread so a child that refuses to die is abandoned (and the existing stuck-process check reports it) rather than crashing shutdown. The reaper blocks in waitpid(), not an interruption point, so interrupt is false. (Win32ChildProcess's pipe-reader timed_joins are intentionally left as-is: one reader writes an outer stack buffer by reference, so detaching it on timeout would be a use-after-scope -- it needs a different fix, not this mechanical migration.) Addresses #18018.
1706513 to
b509132
Compare
Code review findingsImportant
Suggestions
|
🍎 macOS Desktop E2E · ARM64✅ All tests passed!
📥 Download Playwright report⬇️ playwright-report.zip — extract and open
2 shards · macos-26 ARM64 · commit |
|
Thanks for the thorough pass. Addressed in 0c14a79, with notes on the rest: #4 (server_rpc::stop only in exitEarly) is already resolved by #1, #6 done: reworded the test comment so the trailing-slash defect reads as "found while investigating #18018" rather than being #18018, and documented why the inner fallback #2 done: added #5 done: added a binary+platform+trailing-slash case. #7 done: added an inline note that #3 considered but left as-is: the new tests consume the #8 appears moot post- #9 pre-existing and out of scope for this PR. |
🪟 Windows Desktop E2E · Server 2025 x64✅ All tests passed!
📥 Download Playwright report⬇️ playwright-report.zip — extract and open
2 shards · Windows Server 2025 x64 · commit |
Problem
RStudio Desktop on Windows hangs indefinitely when switching projects, closing a project, or opening a project from "no project". Reproduces on
2026.05.1+225("Golden Wattle"), not on2026.05.0+218.Root cause
server_rpc's vulnerability/RPCio_contextworker thread (SessionServerRpc.cpp) is a file-scope global, launched detached behind a perpetualmake_work_guard, and was never stopped. On project switch/close the desktop session quits viaexitEarly()->::exit(), which runs static/global destructors. So~io_context()runs on the main thread while the detached worker is still insiderun()-- undefined behavior that characteristically hangs on the Windows IOCP backend.This started hitting ordinary desktop users because of two commits between
+218and+225:server_rpc::ioContext().isPpmIntegrationEnabled()env-var gate, so the fetch now fires for any session whosegetOption("repos")is a Posit Package Manager URL (e.g.packagemanager.posit.co) -- exactly the affected population.SessionPPMis the only caller ofserver_rpc::ioContext(), so before these commits the worker thread never started on desktop and::exittore down an idle, never-run()context harmlessly.Fix
The hang:
server_rpcworker andstop()+ join it fromexitEarly().core::thread::joinOrAbandonThread()helper that performs the bounded shutdown join: optional interrupt,timed_join, anddetach()on timeout so the exit path never trades the teardown hang for an indefinite join hang. It never throws (safe from a destructor), and requires callers to state whether the thread needs interrupting (io_context workers are not interruption points and must be released viaio_context::stop()).io_contextworker (it wasstop()'d but never joined).OfflineService,ClientEventService, file monitor,ConsoleInputService,HttpConnectionListener, and therserverchild-reaper inServerMain. This also fixes latentstd::terminates where a join timeout left a thread joinable (ConsoleInputService's destructor; therserverreaper).Win32ChildProcess's pipe-readertimed_joins are intentionally left as-is -- one reader writes an outer stack buffer by reference, so detaching on timeout would be a use-after-scope and needs a different fix.Related defect found during investigation (trailing-slash detection):
.rs.ppm.fromRepositoryUrlmatched with a fully-anchored regex ending in(?<snapshot>[^/]+)$, so a PPM repo URL with a trailing slash (e.g..../cran/latest/) did not match. RStudio's own CRAN-mirror UI normalizes repo URLs with a trailing slash, so users who configured a PPM repo that way silently received no vulnerability badges -- the feature [Golden Wattle] drive ppm vulnerability display off the session repositories, not env vars #17823 was meant to deliver to desktop. The detection regex now tolerates an optional trailing slash.Testing
rstudio-tests --scope r --filter ppmpasses, including a newfromRepositoryUrlcase covering the trailing-slash form.Addresses #18018.