Tcl Source Code

View Ticket
Login
Ticket UUID: 9471e6e3042c18f3c8403b7013ecdf104efb5b6d
Title: InitWinEnv not thread safe
Type: Bug Created on: 2016-02-15 10:54:11
Submitter: christian.nassau Assigned to: jan.nijtmans
Subsystem: 08. Environment Variables Severity: Severe
Priority: 6 Last modified: 2019-03-07 08:38:45
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2019-03-07 08:38:45
Version: 8.6.3
Description:
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.
User Comments:
christian.nassau added on 2018-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]]
 			}

chw added on 2018-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 added on 2019-03-07 08:38:45:
Should be fixed now in all branches.

Thanks to Christian Werner for his fix proposal!