Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-21
10:03 • Closed ticket [17902c1882]: env(PATHEXT) not present plus 4 other changes artifact: 5dccdf3277 user: serhiy.storchaka
2019-03-07
08:38 • Closed ticket [9471e6e304]: InitWinEnv not thread safe plus 6 other changes artifact: 1d76bffb00 user: jan.nijtmans
07:58
Fix [9471e6e304]: InitWinEnv not thread safe check-in: e8d0d31e48 user: jan.nijtmans tags: core-8-6-branch
2019-02-12
13:42 • Ticket [9471e6e304] InitWinEnv not thread safe status still Open with 3 othe... artifact: d45e812c57 user: jan.nijtmans
2018-12-16
09:21 • Ticket [9471e6e304]: 3 changes artifact: 67017cf3d1 user: chw
2018-12-15
15:17 • Ticket [9471e6e304]: 4 changes artifact: a21f34102c user: christian.nassau
2016-02-15
10:54 • New ticket [9471e6e304]. artifact: 71f3079625 user: christian.nassau

InitWinEnv not thread safe

Closed / Fixed9471e6e3042c18f3c8403b7013ecdf104efb5b6dCreated 11 years agoUpdated 8 years ago
Descriptionplain2016-02-15 10:54:11
We're seeing the following error during the simultaneous creation of several Tcl interpreters on Windows:

   Can't find a usable init.tcl in the following directories:
       {D:\ourniceproject\tcl8.6}

   D:/ourniceproject/tcl8.6/init.tcl: can't read "env(ComSpec)": no such element in array
   can't read "env(ComSpec)": no such element in array
       while executing
   "set temp $env($p)"
       (procedure "InitWinEnv" line 9)
       invoked from within
   "InitWinEnv"
   ...

The problem seems to be that two simultaneous calls to "InitWinEnv" both try to replace "env(ComSpec)" with "env(COMSPEC)". The relevant code section is

                 set temp $env($p)
                 unset env($p)
                 set env($u) $temp

The "unset env($p)" from the first thread here leads to the error in the second thread.
Referenced by2
christian.nassauplain2018-12-15 15:17:37
Here is a fix; I have no idea where to submit this "officially". 

Index: library/init.tcl
==================================================================
--- library/init.tcl
+++ library/init.tcl
@@ -96,13 +96,17 @@
 		set u [string toupper $p]
 		if {$u ne $p} {
 		    switch -- $u {
 			COMSPEC -
 			PATH {
-			    set temp $env($p)
-			    unset env($p)
-			    set env($u) $temp
+			    # need to catch access to env since it is a volatile
+			    # array that can be changed by another thread
+			    # (ticket [9471e6e3042c18f3c8403b7013ecdf104efb5b6d])
+			    if {![catch {set temp $env($p)}]} {
+			        unset -nocomplain env($p)
+			        set env($u) $temp
+			    }
 			    trace add variable env($p) write \
 				    [namespace code [list EnvTraceProc $p]]
 			    trace add variable env($u) write \
 				    [namespace code [list EnvTraceProc $p]]
 			}
chwplain2018-12-16 09:21:33
I believe the InitWinEnv in init.tcl can be completely left out when
some WIN32 specific logic is added to tclEnv.c, as shown in

 https://www.androwish.org/index.html/artifact/94ec044a94ead074?ln=138-149

Eventually, as a by catch this doesn't have threading issues.
jan.nijtmans Ticket [9471e6e304]: 3 changes [History diff]2019-02-12 13:42:45
jan.nijtmansplain2019-03-07 08:38:45
Should be fixed now in all branches.

Thanks to Christian Werner for his fix proposal!
Log in (anonymous is fine) to add a comment.