<i>Concurrent Tcl_Init calls race in Unix locale initialization, causing crashes on OpenBSD</i>.
by
.
D 2026-10-13T16:28:07.368
J assignee nobody
J closer nobody
J cmimetype text/x-markdown
J comment For\ssome\sreason\sGustaf's\sbug\sreport\sisn't\sshowing\sup\sin\sthe\sbug\slist.\sI\scan't\ssee\sit\s--\sClose\sthis\sone\sif\sit\sis?\r\n\r\n--\sOG\sTicket\sLink:\shttps://core.tcl-lang.org/tcl/tktedit/c35e347c6\swith\sattachements\sof\sfix\sby\sGustaf.\r\n\r\nTitle:\sConcurrent\sTcl_Init\scalls\srace\sin\sUnix\slocale\sinitialization,\scausing\scrashes\son\sOpenBSD\r\n\r\nConcurrent\sinitialization\sof\sTcl\sinterpreters\scan\scause\sa\sdouble\sfree\sor\suse-after-free\sin\s`setlocale()`.\sI\sreproduced\sthis\swith\sstandalone\sTcl\s9.1.0\son\sOpenBSD\sand\sverified\sthe\sattached\spatch\sagainst\sthe\sreproducer.\r\n\r\nThe\sbug\swas\sinitially\sobserved\sin\sNaviServer\swhere\sit\scan\sbe\smitigated\sby\sforcing\sserialized\sinterpreter\screation\svia\sparamter\sseetings\s(ns_param\stclinitlock\strue).\sThe\sattached\sscript\striggeres\sthe\ssame\scrash\swith\splain\sTcl\s9.1.0.\r\n\r\n###\sEnvironment\r\n\r\n-\sTcl\s9.1.0,\sbuilt\sfrom\sthe\srelease\ssources\swith\s`--enable-symbols`\r\n-\sThread\sextension\s3.0.7,\sbundled\swith\sTcl\r\n-\sOpenBSD\s7.9,\sarm64\r\n-\sUTM\sVM\son\san\sApple\sM1,\susing\shardware\svirtualization\r\n\r\nThe\soriginal\sreport\scame\sfrom\sa\sNaviServer\suser\son\sOpenBSD\samd64,\swhose\sbacktraces\sshowed\sthe\ssame\sTcl\sinitialization\spath.\sThe\sattached\sstandalone\sreproducer\srequires\sno\sNaviServer.\r\n\r\n###\sReproduction\r\n\r\nThe\sattached\s`tcl-thread-init-race.tcl`\sestablishes\seight\spersistent\screator\sthreads.\sThey\srendezvous\sbefore\seach\sround,\sthen\seach\screates\sand\sjoins\sa\sfresh\schild\sthread.\sChild\sinitialization\sinvokes\s`Tcl_Init()`\sthrough\sthe\sThread\sextension.\r\n\r\nRun:\r\n\r\n```sh\r\n\s\s\stclsh9.1\stcl-thread-init-race.tcl\sserialized\s8\s50\r\n\s\s\stclsh9.1\stcl-thread-init-race.tcl\sparallel\s8\s50\r\n```\r\n\r\nThe\sserialized\scontrol\sprotects\s`thread::create`\swith\sa\sshared\smutex.\sSince\s`thread::create`\swaits\sfor\schild\sinitialization\sto\sfinish,\sthis\sserializes\sthe\schild\sinitialization.\r\n\r\nResults\swith\sunmodified\sTcl\s9.1.0:\r\n\r\n```text\r\n\s\s\smode=serialized\screators=8\srounds=50\r\n\s\s\scompleted=400\sfailed_creators=0\s(no\scrash\sobserved)\r\n```\r\n\r\n```text\r\n\s\s\smode=parallel\screators=8\srounds=50\r\n\s\s\stclsh9.1(54092)\sin\sfree():\sdouble\sfree\s0x23c0cc14b0\r\n\s\s\sAbort\strap\s(core\sdumped)\r\n```\r\n\r\n###\sBacktrace\r\n\r\nRelevant\sframes\sfrom\sthe\sstandalone\sTcl\scrash:\r\n\r\n```text\r\n_libc_free\r\nfreegl\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\slibc/locale/setlocale.c:33\r\n_libc_setlocale\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\slibc/locale/setlocale.c:178\r\nTcl_GetEncodingNameFromEnvironment\s\sunix/tclUnixInit.c:591\r\nTclpSetInitialEncodings\s\s\s\s\s\s\s\s\s\s\s\s\sunix/tclUnixInit.c:540\r\nTcl_Init\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sgeneric/tclInterp.c:845\r\nNewThread\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sthread3.0.7/generic/threadCmd.c:1857\r\nNewThreadProc\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sgeneric/tclEvent.c:2070\r\n_rthread_start\r\n```\r\n\r\nThe\soriginal\sNaviServer\sreport\salso\sshowed\sa\scrash\sin\s`strlen()`\scalled\sby\s`strdup()`\sinside\s`setlocale()`,\sreached\sthrough\sthe\ssame\sTcl\sfunctions.\r\n\r\n###\sCause\r\n\r\n`Tcl_Init()`\scalls\s`TclpSetInitialEncodings()`,\swhich\scalls\s`Tcl_GetEncodingNameFromEnvironment()`.\sUnder\s`HAVE_LANGINFO`,\sthat\sfunction\sperforms:\r\n\r\n```c\r\n\s\s\ssetlocale(LC_CTYPE,\s"")\r\n```\r\n\r\nfollowed\sby\s`nl_langinfo(CODESET)`\sand\sa\scopy\sof\sthe\sreturned\sstring.\r\n\r\nThis\ssequence\sis\snot\ssynchronized\sbetween\sconcurrent\sinterpreter\sinitializations.\s`setlocale()`\smodifies\sprocess-global\sstate\sand\sis\snot\srequired\sby\sPOSIX\sto\sbe\sthread-safe.\sOpenBSD’s\simplementation\scan\sfree\sand\sreplace\sshared\slocale\sstorage,\sexplaining\sthe\sobserved\sdouble\sfree\sand\suse-after-free.\r\n\r\nThe\sinitial\slocale\ssetup\sin\s`Tcl_InitSubsystems()`\salready\suses\s`TclpInitLock()`,\sbut\sthe\srepeated\slocale\sdetection\sduring\sinterpreter\sinitialization\sdoes\snot.\r\n\r\n###\sProposed\sfix\r\n\r\nThe\sattached\spatch\sfor\sTcl\s9.1.0\suses\sthe\sexisting\s`TclpInitLock()`\s/\s`TclpInitUnlock()`\saround:\r\n\r\n1.\s`setlocale(LC_CTYPE,\s"")`\r\n2.\s`nl_langinfo(CODESET)`\r\n3.\sCopying\sthe\scodeset\sinto\sa\sTcl\sDString\r\n\r\nIt\sreleases\sthe\slock\sbefore\sresolving\sthe\scopied\sencoding\sname\sor\scalling\s`Tcl_GetEncoding()`.\r\n\r\nThis\scoordinates\sthe\srepeated\slocale\sdetection\swith\sTcl’s\sinitial\slocale\ssetup\swithout\sserializing\sthe\swhole\sof\s`Tcl_Init()`.\sThe\sexisting\slocale-failure\sfallback\sand\sweak-import\shandling\sare\spreserved.\r\n\r\nThe\spatch\sis\sintentionally\snot\sOpenBSD-specific.\sOther\sUnix\slibc\simplementations\smay\salso\srequire\ssynchronization,\salthough\sthe\scrash\sdescribed\shere\sis\sconfirmed\son\sOpenBSD.\sThe\slock\scoordinates\sTcl’s\sown\scalls;\sit\scannot\scoordinate\sunrelated\slibraries\scalling\s`setlocale()`\sindependently.\r\n\r\n###\sValidation\r\n\r\nAfter\sapplying\sthe\spatch,\srebuilding,\sand\sreinstalling\sTcl:\r\n\r\n```sh\r\n\s\s\stclsh9.1\stcl-thread-init-race.tcl\sparallel\s8\s500\r\n\s\s\stclsh9.1\stcl-thread-init-race.tcl\sserialized\s8\s500\r\n```\r\n\r\nBoth\sruns\scompleted\ssuccessfully:\r\n\r\n```text\r\n\s\s\smode=parallel\screators=8\srounds=500\r\n\s\s\scompleted=4000\sfailed_creators=0\s(no\scrash\sobserved)\r\n```\r\n\r\n```text\r\n\s\s\smode=serialized\screators=8\srounds=500\r\n\s\s\scompleted=4000\sfailed_creators=0\s(no\scrash\sobserved)\r\n```\r\n\r\nThese\sstress\sruns\ssupport\sthe\sfix;\sthey\sdo\snot\sestablish\sthat\severy\spossible\sconcurrency\sissue\sis\sabsent.
J foundin 9.1.0
J is_private 0
J login mookie
J priority 5\sMedium
J private_contact eb7513005aa11dbe2a07d8bff76f8af3c25f379b
J resolution None
J severity Severe
J status Open
J submitter mookie
J subsystem 20.\s[interp]
J title Concurrent\sTcl_Init\scalls\srace\sin\sUnix\slocale\sinitialization,\scausing\scrashes\son\sOpenBSD
J type Bug
K 55a1568dfc2fce102552e388939f0fe3cdbf6c15
U mookie
Z 8de263b72f0f170fac22d486297cdf45