Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 086f1258bb7898a5301bb7ee97ddc613b0a6920da4b4274d768c377417658d65
Ticket: c35e347c6fb8df66917b1ab6974b3638925b4aa2
Concurrent Tcl_Init calls race in Unix locale initialization, causing crashes on OpenBSD
User & Date: gustafn3 on 2026-10-20 08:38:07
Changes

  1. icomment:
    Ashok. yes, process-wide setup would be preferable. From the embedding application's perspective, there is no need to reset the process locale whenever another interpreter is initialized. 
    
    The expected performance differences depend on the initialization scope:
    
    - Per-interpreter setup repeats locale detection for every interpreter.
    - Per-thread setup reduces that work when a thread creates multiple interpreters, but still repeats process-global locale operations for each thread.
    - Per-process setup avoids both forms of repetition.
    
    On OpenBSD, the current unsynchronized calls can crash, so synchronization is required if those calls remain. A per-interpreter lock would serialize the locale-detection section; a per-thread approach would reduce contention only where multiple interpreters share each thread. Note, that the submitted crash-producer creates one interpreter per fresh child thread, so per-thread initialization alone would not reduce its locale-call count.
    
    On glibc-based Linux and macOS, the libc implementations already lock locale updates internally. Repeating those calls therefore already incurs synchronization, even without an additional Tcl lock. Avoiding redundant calls could benefit probably those platforms as well. Also, NaviServer will benefit from this.
    
    The apparent reason for repeated calls is the startup sequencing described above TclpSetInitialEncodings(): an early attempt may lack the information needed to load the appropriate Tcl encoding. That seems to justify retrying encoding resolution, rather than repeating libc locale setup.
    
    Could those steps be separated—capturing the locale/codeset during the existing protected initialization in Tcl_InitSubsystems(), then resolving the corresponding Tcl encoding later if necessary?
    
    My patch was intended as a minimal correctness fix for the demonstrated race under OpenBSD; I would welcome an initialization change that removes the repeated work. The public Tcl_GetEncodingNameFromEnvironment() and Tcl_GetEncodingNameForUser() entry points would still need their behavior and concurrent use considered separately.
    
    Thanks for looking into this!
    
  2. login: "gustafn3"
  3. mimetype: "text/x-markdown"