Skip to content
Tcl Source Code View Ticket
Login

View Ticket

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.