Repository navigation
fix dangling setlocale restore and log locale sync failures on windows - #18141
Merged
Merged
Conversation
Two Windows locale-state hazards found while investigating console UTF-8 corruption with non-ASCII locale names (Turkish_Türkiye.utf8, Norwegian Bokmål_Norway.utf8): - utf8ToConsole() saved the pointer returned by setlocale(LC_CTYPE, NULL) and restored from it after switching to the "C" locale. The pointer refers to an internal CRT buffer that a subsequent setlocale() call may overwrite, so the restore could permanently pin the session's LC_CTYPE to "C". Copy the locale name before switching. rsession and R share ucrtbase.dll CRT state, so this clobbers R's locale too. - synchronizeLocale() ignored the return value of setlocale(LC_ALL, ...). A failed LC_ALL update (possible with locale names containing non-ASCII characters) may leave only some categories applied; log the failure so affected sessions are diagnosable. Addresses #18139
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 |
Force Visual Studio 2026 / MSVC 14.5 everywhere on Windows and drop VS 2022: - default the boost/SOCI prebuilt toolset to msvc145 and the SOCI CMake generator to "Visual Studio 18 2026"; update the Jenkins Dockerfile and prereqs installer to install VS 2026 (ltsc2022 base image) - locate Visual Studio via vswhere instead of hardcoded install paths, and drop the CI junction hacks that existed to satisfy the old path lookup - remove the msvc143 fallback from CMakeLists now that msvc145 is required - pin rstudio-zlib's archive output to a flat path so the build links under both the Ninja and Visual Studio generators - add cmake/windows-checks.cmake to validate compiler, architecture, and generator up front, with actionable messages (plus friendlier boost/SOCI "dependencies not installed" errors) Addresses #17986.
kevinushey
force-pushed
the
bugfix/win-locale-setlocale-fixes
branch
from
July 4, 2026 02:36
b03e10c to
0b66a07
Compare
- save and restore LC_CTYPE via the wide _wsetlocale() variant so locale names containing non-ASCII characters (for example Turkish or Norwegian Bokmal locale names on Windows 11) survive the round-trip while the conversion loop runs in the "C" locale - align the synchronizeLocale() failure log message with the _wsetlocale() call it now reports on Addresses #18139.
- point Jenkinsfile.windows at the 2026 BuildTools VsDevCmd.bat, matching the install location used by Dockerfile.windows and Install-RStudio-Prereqs.ps1 - fail CMake configure early, with the actual requirement, when the detected MSVC toolset does not match the msvc145 prebuilts; previously a VS 2022 user hit the Boost "prebuilts not found" error whose advice (re-run install-dependencies.cmd) could never resolve it - fix the setlocale() save/restore in the tokenizer unicode test: it saved the pointer returned when setting C.UTF-8 (the new locale, and a pointer into a buffer later calls may clobber) and so restored the wrong locale
tools::parse_Rd() loads user-macro definitions from R's system.Rd by default, and errors when a minimal R installation ships without that file (as seen on CI). The Rd fixtures in these tests use only base Rd tags, so fall back to an empty user-macro set -- loaded from an empty file so the macro environment is structurally valid -- which parses these fixtures identically.
On Linux, the tokenizer classifies unicode letters with iswalnum(), which only recognizes non-ASCII letters in a unicode locale. This test had been passing there by accident: the earlier identifier test leaked its C.UTF-8 locale into the process because its save/restore was broken (it restored the newly-set locale). Now that the leak is fixed, set the locale explicitly around tokenization -- and proceed rather than skip when C.UTF-8 is unavailable, since macOS (CFCharacterSet) and Windows (UCRT) classify these independently of the narrow locale.
gtritchie
added a commit
that referenced
this pull request
Jul 6, 2026
Restore the Windows build configuration to e4e2b13, the last commit with a green Windows build, backing out the VS 2026 / MSVC 14.5 toolchain migration that landed alongside the setlocale fix in #18141 (the #17986 work). The migration bumped the Windows Docker base image to windowsservercore-ltsc2022, but the Jenkins Windows agents run Windows Server 2019 (10.0.17763). A 2022-based (10.0.20348) container cannot run there under process isolation, so the image rebuild fails at the FROM step, the jenkins/ide:windows-x86_64-pacific-dogwood tag in ECR stays at the pre-migration image, and downstream builds pull that stale VS 2022 image -- which windows-checks.cmake then rejects for reporting toolset 143. main has been red on Windows since the migration merged. Only the toolchain hunks are reverted. The #18139 setlocale fix and its test fixes from #18141 are preserved, as are the unrelated e2e cache changes from #18151. The migration can reland once the Windows CI fleet is upgraded to Server 2022.
kevinushey
added a commit
that referenced
this pull request
Aug 27, 2026
* repair the windows locale when it is changed behind r's back
rsession and UCRT builds of R (R >= 4.2) share a single C runtime, so a
setlocale() call anywhere in the process moves R's locale too -- without
R's cached view of the encoding (localeCP, utf8locale, native_enc, all
computed in R_check_locale) being updated to match.
Code that saves the locale, switches to "C" and restores is a ready
source of exactly that, because the saved name is narrow: a locale name
holding non-ASCII characters (Turkish_Türkiye.utf8 on Windows 11,
Norwegian Bokmål_Norway.utf8) cannot be parsed back once the process is
in the "C" locale, so the restore fails and the session is left in "C"
while R goes on reporting a UTF-8 locale. This is reproducible with R
alone, RStudio not involved:
Sys.setlocale("LC_CTYPE", "Norwegian Bokmål_Norway.utf8")
before <- Sys.getlocale("LC_CTYPE")
Sys.setlocale("LC_CTYPE", "C")
Sys.setlocale("LC_CTYPE", before) # fails; session stays in "C"
- synchronizeLocale() now watches for that divergence on each R busy
transition and repairs it, restoring a wide copy of the locale
recorded while R and the C runtime last agreed (or re-adopting the
operating system's locale when the divergence predates any such
observation, which is what affected users have been doing by hand with
Sys.setlocale("LC_ALL", "")). R's localeCP is the arbiter, so a
deliberate Sys.setlocale() -- which moves R and the C runtime together
-- is left alone.
This replaces the old codepage-equality guard, which disabled the
function in exactly this case: localeCP never changes while the
locale drifts, so the two always compared equal. It also drops the
Sys.getlocale() call the function used to make from inside the busy
callback, and with it a latent bug -- that string is native-encoded,
and it was being read as UTF-8.
- console input is converted with r::util::utf82rconsole(), which
encodes for the code page R reports rather than the one the C runtime
locale implies, so input survives a drift even before it is repaired.
It uses a new explicit-codepage overload of string_utils::
utf8ToSystem() that converts with WideCharToMultiByte() instead of
wctomb(); text destined for a UTF-8 code page is passed through
untouched, which also stops surrogate pairs being split.
WC_NO_BEST_FIT_CHARS means an unrepresentable character is escaped for
R rather than silently transliterated into a lookalike.
- correct the comment on the startup _wsetlocale() added in #18141: R
adopts the operating system locale itself at the top of
setup_Rmainloop() on Windows, so that call is not what gives R its
locale. It still matters when a locale environment variable names
something Windows rejects and R's own call fails.
Tests: a core gtest asserting that conversion for a UTF-8 code page is
independent of the C runtime locale, and an rsession gtest asserting
that synchronizeLocale() puts back a locale clobbered behind R's back.
Both are Windows-only, so Windows CI exercises them.
Addresses #18139. Related: #15008.
* Address Windows locale review feedback
* Refine Windows locale repair handling
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.
Addresses #18139. Related: #15008.
While investigating #18139 (console corrupts UTF-8 input at startup on Windows 11 with the Turkish locale, until
Sys.setlocale()is called), I found two Windows locale-state hazards on the RStudio side. Both matter because rsession (MSVC,/MD) and UCRT builds of R share a singleucrtbase.dllCRT instance, so an rsession-sidesetlocale()silently clobbers R's locale without R's internal state (localeCP,utf8locale,native_enc) being updated.1. Dangling
setlocale()pointer restore inutf8ToConsole()SessionRMarkdown.cppsaved the pointer returned bysetlocale(LC_CTYPE, NULL), switched to the"C"locale for the conversion loop, then "restored" from the saved pointer. That pointer refers to an internal CRT buffer which a subsequentsetlocale()call may overwrite, so the restore could permanently pin the session'sLC_CTYPEto"C"after rendering an R Markdown document. The locale name is now copied into astd::stringbefore switching, and a failed restore is logged.2.
synchronizeLocale()ignoredsetlocale()failurer::util::synchronizeLocale()(runs on every R busy transition) round-trips the compoundSys.getlocale("LC_ALL")string back into::setlocale(LC_ALL, ...)and discarded the return value. Locale names containing non-ASCII characters (Turkish_Türkiye.utf8on Windows 11,Norwegian Bokmål_Norway.utf8in #15008) are suspected to fail this round-trip, and a failedLC_ALLupdate can leave only some categories applied. Failures are now logged with the requested and resulting locale so affected sessions are diagnosable.Notes
#ifdef _WIN32, so local macOS builds don't exercise them; relying on the Windows CI build for compile verification.