Repository navigation
repair the windows locale when it is changed behind r's back - #18657
Merged
Merged
Conversation
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.
gtritchie
reviewed
Aug 26, 2026
gtritchie
reviewed
Aug 26, 2026
gtritchie
reviewed
Aug 26, 2026
gtritchie
reviewed
Aug 26, 2026
gtritchie
reviewed
Aug 26, 2026
gtritchie
reviewed
Aug 26, 2026
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:41 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:41 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:46 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:46 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:49 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:49 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:53 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:53 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:53 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:53 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:53 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 04:53 — with
GitHub Actions
Inactive
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 |
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:19 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:19 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:20 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:20 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:26 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:26 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:34 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:34 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:34 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:34 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:34 — with
GitHub Actions
Inactive
kevinushey
temporarily deployed
to
e2e-playwright
August 27, 2026 20:34 — with
GitHub Actions
Inactive
This branch was previously deployed
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.
What is actually going wrong
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 -- but silently, without R's cached view of the encoding (localeCP,utf8locale,native_enc, all computed inR_check_locale) being updated to match.Code that saves the locale, switches to
"C"for a conversion and then restores it is a ready source of exactly that, because the saved name is narrow. A locale name holding non-ASCII characters --Turkish_Türkiye.utf8on 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 part is reproducible with R alone, RStudio not involved (confirmed on Windows 11, system code page 65001):
Marking the encoding on
beforedoes not help; UCRT rejects the name however it is encoded.That mismatch -- C runtime in
"C", R believing UTF-8 -- is what produces the reported symptoms. Console input runs through a per-characterwctomb(), which follows the C runtime locale: in"C",Ç(U+00C7) truncates to a raw0xC7byte andı/şfail and come through as\u131/\u15fescapes, which is the reporter'sError: unexpected '\\' in "al\"exactly. It also explains whySys.setlocale()repaired a session by hand and whyView()broke it again (#15008).The reporter's own diagnostics rule out the theory the previous attempt (#18141) was built on: a fresh broken session reports
Sys.getlocale()as"C"whilel10n_info()reports MBCSTRUE, UTF-8TRUE, code page65001. Those flags come only fromR_check_locale(), and in a"C"locale they would beFALSE/FALSE/0-- so R started in the right locale and the C runtime was moved afterwards.What this changes
synchronizeLocale()detects and repairs the divergence. It runs on each R busy transition; it now compares the code page implied by the C runtime'sLC_CTYPEagainst R'slocaleCPand, when they disagree, restores a wide copy of the locale recorded while the two last agreed -- or re-adopts the operating system's locale when the divergence predates any such observation, which is what affected users have been doing by hand withSys.setlocale("LC_ALL", "").localeCPis the arbiter, so a deliberateSys.setlocale()(which moves R and the C runtime together) is left alone.This replaces the codepage-equality guard, which disabled the function in exactly this case:
localeCPnever changes while the locale drifts, so the two always compared equal. It also drops theSys.getlocale()call the function made from inside the busy callback, and with it a latent bug -- that string is native-encoded and was being read as UTF-8.Console input no longer depends on the C runtime locale. It goes through a new
r::util::utf82rconsole(), which encodes for the code page R reports rather than the oneLC_CTYPEimplies, so input survives a drift even before it is repaired. That uses a new explicit-codepage overload ofstring_utils::utf8ToSystem()converting withWideCharToMultiByte()instead ofwctomb(); text destined for a UTF-8 code page is passed through untouched, which also stops surrogate pairs from being split.WC_NO_BEST_FIT_CHARSmeans an unrepresentable character is escaped for R rather than silently transliterated into a lookalike that means something else.Corrected the comment on the startup
_wsetlocale()added in fix dangling setlocale restore and log locale sync failures on windows #18141. R adopts the operating system locale itself at the top ofsetup_Rmainloop()on Windows (src/main/main.c,#ifdef Win32), so that call is not what gives R its locale. It still earns its place when a locale environment variable names something Windows rejects and R's own call fails, so the call stays; only the reasoning was wrong.What this does not do
It does not find the code that moves the locale in the first place -- narrowing that on an affected machine is still outstanding, and
View()remains the most reliable trigger. This makes the session self-healing rather than eliminating the clobber, so it wants manual verification on a Turkish or Bokmål Windows box before we call #18139 closed.The
Sys.setlocale()half is arguably an R bug:Sys.getlocale()hands back a name thatSys.setlocale()then refuses, where going through_wsetlocale()internally would round-trip. Worth reporting upstream separately.Testing
synchronizeLocale()puts back a locale clobbered behind R's back.Both are Windows-only, so Windows CI is what exercises them; the POSIX side of every touched file compiles locally.