Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 18ab5b1e1c21cf716e895ff0d17e867155a5a69c742c61024901cc20979a110c
Ticket: c35e347c6fb8df66917b1ab6974b3638925b4aa2
Concurrent Tcl_Init calls race in Unix locale initialization, causing crashes on OpenBSD
User & Date: gustafn3 on 2026-10-07 09:19:24
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed to:
    Title: Concurrent Tcl_Init calls race in Unix locale initialization, causing crashes on OpenBSD
    
    Concurrent initialization of Tcl interpreters can cause a double free or use-after-free in `setlocale()`. I reproduced this with standalone Tcl 9.1.0 on OpenBSD and verified the attached patch against the reproducer.
    
    The bug was initially observed in NaviServer where it can be mitigated by forcing serialized interpreter creation via paramter seetings (ns_param tclinitlock true). The attached script triggeres the same crash with plain Tcl 9.1.0.
    
    ### Environment
    
    - Tcl 9.1.0, built from the release sources with `--enable-symbols`
    - Thread extension 3.0.7, bundled with Tcl
    - OpenBSD 7.9, arm64
    - UTM VM on an Apple M1, using hardware virtualization
    
    The original report came from a NaviServer user on OpenBSD amd64, whose backtraces showed the same Tcl initialization path. The attached standalone reproducer requires no NaviServer.
    
    ### Reproduction
    
    The attached `tcl-thread-init-race.tcl` establishes eight persistent creator threads. They rendezvous before each round, then each creates and joins a fresh child thread. Child initialization invokes `Tcl_Init()` through the Thread extension.
    
    Run:
    
    ```sh
       tclsh9.1 tcl-thread-init-race.tcl serialized 8 50
       tclsh9.1 tcl-thread-init-race.tcl parallel 8 50
    ```
    
    The serialized control protects `thread::create` with a shared mutex. Since `thread::create` waits for child initialization to finish, this serializes the child initialization.
    
    Results with unmodified Tcl 9.1.0:
    
    ```text
       mode=serialized creators=8 rounds=50
       completed=400 failed_creators=0 (no crash observed)
    ```
    
    ```text
       mode=parallel creators=8 rounds=50
       tclsh9.1(54092) in free(): double free 0x23c0cc14b0
       Abort trap (core dumped)
    ```
    
    ### Backtrace
    
    Relevant frames from the standalone Tcl crash:
    
    ```text
    _libc_free
    freegl                              libc/locale/setlocale.c:33
    _libc_setlocale                     libc/locale/setlocale.c:178
    Tcl_GetEncodingNameFromEnvironment  unix/tclUnixInit.c:591
    TclpSetInitialEncodings             unix/tclUnixInit.c:540
    Tcl_Init                           generic/tclInterp.c:845
    NewThread                          thread3.0.7/generic/threadCmd.c:1857
    NewThreadProc                      generic/tclEvent.c:2070
    _rthread_start
    ```
    
    The original NaviServer report also showed a crash in `strlen()` called by `strdup()` inside `setlocale()`, reached through the same Tcl functions.
    
    ### Cause
    
    `Tcl_Init()` calls `TclpSetInitialEncodings()`, which calls `Tcl_GetEncodingNameFromEnvironment()`. Under `HAVE_LANGINFO`, that function performs:
    
    ```c
       setlocale(LC_CTYPE, "")
    ```
    
    followed by `nl_langinfo(CODESET)` and a copy of the returned string.
    
    This sequence is not synchronized between concurrent interpreter initializations. `setlocale()` modifies process-global state and is not required by POSIX to be thread-safe. OpenBSD’s implementation can free and replace shared locale storage, explaining the observed double free and use-after-free.
    
    The initial locale setup in `Tcl_InitSubsystems()` already uses `TclpInitLock()`, but the repeated locale detection during interpreter initialization does not.
    
    ### Proposed fix
    
    The attached patch for Tcl 9.1.0 uses the existing `TclpInitLock()` / `TclpInitUnlock()` around:
    
    1. `setlocale(LC_CTYPE, "")`
    2. `nl_langinfo(CODESET)`
    3. Copying the codeset into a Tcl DString
    
    It releases the lock before resolving the copied encoding name or calling `Tcl_GetEncoding()`.
    
    This coordinates the repeated locale detection with Tcl’s initial locale setup without serializing the whole of `Tcl_Init()`. The existing locale-failure fallback and weak-import handling are preserved.
    
    The patch is intentionally not OpenBSD-specific. Other Unix libc implementations may also require synchronization, although the crash described here is confirmed on OpenBSD. The lock coordinates Tcl’s own calls; it cannot coordinate unrelated libraries calling `setlocale()` independently.
    
    ### Validation
    
    After applying the patch, rebuilding, and reinstalling Tcl:
    
    ```sh
       tclsh9.1 tcl-thread-init-race.tcl parallel 8 500
       tclsh9.1 tcl-thread-init-race.tcl serialized 8 500
    ```
    
    Both runs completed successfully:
    
    ```text
       mode=parallel creators=8 rounds=500
       completed=4000 failed_creators=0 (no crash observed)
    ```
    
    ```text
       mode=serialized creators=8 rounds=500
       completed=4000 failed_creators=0 (no crash observed)
    ```
    
    These stress runs support the fix; they do not establish that every possible concurrency issue is absent.
    
  5. foundin changed to: "9.1.0"
  6. is_private changed to: "0"
  7. login: "gustafn3"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "ef1993a98c1daa778fe0b246a7af12b3076f2240"
  10. resolution changed to: "None"
  11. severity changed to: "Severe"
  12. status changed to: "Open"
  13. submitter changed to: "gustafn3"
  14. subsystem changed to: "20. [interp]"
  15. title changed to:
    Concurrent Tcl_Init calls race in Unix locale initialization, causing crashes on OpenBSD
    
  16. type changed to: "Bug"