Repository navigation
initialize COM at rsession startup on Windows - #18237
Merged
Merged
Conversation
Since R 4.3.0, askcdstring() (behind utils::choose.dir()) uses the COM-based IFileOpenDialog but does not initialize COM itself, so CoCreateInstance fails with CO_E_NOTINITIALIZED and choose.dir() silently returns NA unless some other API (e.g. file.choose()'s GetOpenFileNameW) happened to initialize COM on the thread first. Initialize COM (STA) eagerly at startup so the apartment state is deterministic and choose.dir() works out of the box. Addresses #13078.
Contributor
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since R 4.3.0,
utils::choose.dir()returnsNAwithout showing a dialog whenever R runs outside of Rgui (rsession, Rterm, Rscript). R 4.3 rewroteaskcdstring()insrc/extra/graphapp/dialogs.cto use the COM-basedIFileOpenDialog, but it never initializes COM on the calling thread, soCoCreateInstancefails withCO_E_NOTINITIALIZEDand the failure is silently mapped toNA-- unless some other API happened to initialize COM on the thread first. That's why the known workaround (callingfile.choose()first) works:GetOpenFileNameWinitializes COM as a side effect. It's also why the bug never reproduced on some machines:detectGitBinDirFromShortcut()in SessionGit.cpp callsCoInitialize()when git isn't found on the PATH.This PR initializes COM (STA, the model used by the common dialogs and Rgui) eagerly at rsession startup on Windows, so the main thread's apartment state is deterministic instead of depending on which code path runs first. Per COM semantics this is safe with respect to later initializers: they receive
S_FALSE(already initialized) and proceed normally. The initialization is intentionally process-lifetime, so there is no matchingCoUninitialize.The root cause has also been reported upstream to R (an
askcdstring()fix would only land in future R versions; this change fixes the behavior for all R >= 4.3.0).Verification: requires a Windows build; running
utils::choose.dir()in a fresh session should show the folder picker instead of returningNAimmediately. I've verified the equivalent state manually via thefile.choose()workaround (which produces exactly the COM state this change sets up eagerly), but the change itself still needs a Windows smoke test.Addresses #13078.