Skip to content

repair the windows locale when it is changed behind r's back - #18657

Merged
kevinushey merged 5 commits into
mainfrom
bugfix/win-locale-drift-repair
Aug 27, 2026
Merged

kevinushey merged 5 commits into
mainfrom
bugfix/win-locale-drift-repair

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

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 in R_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.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 part is reproducible with R alone, RStudio not involved (confirmed on Windows 11, system code page 65001):

Sys.setlocale("LC_CTYPE", "Norwegian Bokmål_Norway.utf8")
before <- Sys.getlocale("LC_CTYPE")
Sys.setlocale("LC_CTYPE", "C")
Sys.setlocale("LC_CTYPE", before)
#> [1] ""
#> Warning message:
#> OS reports request to set locale to "Norwegian Bokmål_Norway.utf8" cannot be honored
Sys.getlocale("LC_CTYPE")
#> [1] "C"

Marking the encoding on before does 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-character wctomb(), which follows the C runtime locale: in "C", Ç (U+00C7) truncates to a raw 0xC7 byte and ı/ş fail and come through as \u131/\u15f escapes, which is the reporter's Error: unexpected '\\' in "al\" exactly. It also explains why Sys.setlocale() repaired a session by hand and why View() 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" while l10n_info() reports MBCS TRUE, UTF-8 TRUE, code page 65001. Those flags come only from R_check_locale(), and in a "C" locale they would be FALSE/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's LC_CTYPE against R's localeCP and, 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 with Sys.setlocale("LC_ALL", ""). localeCP is the arbiter, so a deliberate Sys.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: localeCP never changes while the locale drifts, so the two always compared equal. It also drops the Sys.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 one LC_CTYPE implies, so input survives a drift even before it is repaired. That uses a new explicit-codepage overload of string_utils::utf8ToSystem() converting with WideCharToMultiByte() instead of wctomb(); text destined for a UTF-8 code page is passed through untouched, which also stops surrogate pairs from being split. WC_NO_BEST_FIT_CHARS means 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 of setup_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 that Sys.setlocale() then refuses, where going through _wsetlocale() internally would round-trip. Worth reporting upstream separately.

Testing

  • A core gtest asserting that conversion for a UTF-8 code page is independent of the C runtime locale, and that an unrepresentable character is escaped rather than transliterated.
  • An rsession gtest asserting that 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.

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.
@kevinushey kevinushey added this to the 2026.09 (Autumn Hawkbit) milestone Aug 26, 2026
Comment thread src/cpp/r/RUtil.cpp Resolved
Comment thread src/cpp/core/StringUtils.cpp Resolved
Comment thread src/cpp/r/RUtil.cpp Resolved
Comment thread src/cpp/r/RUtil.cpp Resolved

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feedback posted

@github-actions

github-actions Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
2438 0 152 0
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 606 0 31 0
✅ desktop-macos 612 0 26 0
✅ desktop-windows 612 0 31 0
✅ server-linux 580 0 44 0
✅ setup 26 0 10 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

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


All platforms - commit 9d9c614c8da531372bdb7b2ee2cfeb093e2313e5 - Run #722

@kevinushey
kevinushey merged commit 3a2e8a7 into main Aug 27, 2026
29 checks passed
@kevinushey
kevinushey deleted the bugfix/win-locale-drift-repair branch August 27, 2026 21:28

This branch was previously deployed

1 inactive deployment
e2e-playwright — 9d9c614c Deployed Aug 27, 2026 by kevinushey via windows / e2e-desktop-windows (3) #722
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.

2 participants