Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 518d2f3a1ec59b1fc6528b6ac3f5b27d5e3c50ba035addc724d9deb8da1e92f4
Ticket: 61c01e0edb08a9ed905aefdf17ce113da303c77b
likely race condition with package require in several threads causing error
User & Date: anonymous on 2025-05-04 06:28:58
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    Summary: 
    
    There appears to be a race condition bug that occurs randomly, but not always, using packages from within several threads with version 9. Failures can take several program executions to occur, but usually less than a minute at about 1 run per second. The failure presents as a "can't find package" error.
    
    Detail:
    
    In tcl/tk 9.0.1 I am seeing what is highly likely a race condition bug with threads and packages that did not occur in 8.6. I have only run this on windows 10. The error occurs only inside a thread, and typically there needs to be 3 or more threads started before an error occurs, which is a failure to find a package. There are several packages I've had the error with, but the math package is usually the fastest to fail, and so is used in the below test script. The error occurs randomly. Some distro's see the error more frequently. One, a bawt tclkit, results in an access violation crash. The magicsplat 8.6 did not fail at all in over 1000 runs.
    
    Enclosed below are a windows batch script and a tcl script. The batch script runs the tcl program in a loop, which takes about 1 second if it succeeds. When it fails to load the math package, it will display a tk dialog box which suspends the program and the console is open and available and the failing and working threads are still active and can receive thread::send requests for additional debugging.
    
    When it fails, the auto_path variable (in the failing thread) does not have an entry for math. In looking at the pkgIndex script for math, there is the item:
    
    package ifneeded math  1.2.6 [list source [file join $dir math.tcl]]
    
    which by instrumenting these scripts, it was determined that this was indeed executed (in all the threads), and the script is correct, however, by instrumenting math.tcl, it was found that when the error "can't find package math" occurs, the math.tcl script had not (yet?) been executed. 
    
    However, doing a [package ifneeded math 1.2.6] does return the script, in all threads, after the error occurs. 
    
    If the command [package require math] is run a second time, it will work. Another workaround is to do a bogus package require on a non-existent package, and it then it will (usually) work. By reducing to two threads, or adding a delay before running the third thread, it would also not fail (at least with running 1500 times or so it didn't fail).
    
    Although somewhat lengthy, this is the smallest script I could use to reliably get the error in a timeframe of seconds to minutes.
    
    The script will dump out the auto_path (from the main thread) on startup. Then there is a proc called tdump which is used to view which of the several threads (aka tasks) is in error and the tid's for each. One can then use thread::send in the console to get info back from the thread whose package require failed, as shown below. 
    
    Note that once it works (i.e. after say doing the package require a second time) the math entry is then found in the auto_path variable. This is set in the math.tcl file with this code:
    
    		variable home [file join [pwd] [file dirname [info script]]]
    		if {[lsearch -exact $::auto_path $home] == -1} {
    			lappend ::auto_path $home
    		}
    
    
    Here's what the output looks like on a bawt distro when it fails, followed by two commands entered into the console to get the value of auto_path for 2 of the threads.:
    
    
    //zipfs:/lib/tcl/tcl_library
    //zipfs:/lib/tcl
    C:/bawTcl/lib
    C:/bawTcl/lib/cawt
    C:/bawTcl/lib/tcl3d
    C:/bawTcl/lib/tcllib
    C:/bawTcl/lib/tclvfs/template
    //zipfs:/lib/tk/tk_library
    //zipfs:/lib/tk/tk_library/ttk
                     (taskname1,error)           = |can't find package math| 
                     (taskname1,tid)             = |tid0000000000003078| 
                     (taskname2,tid)             = |tid0000000000002F60| 
                     (taskname3,tid)             = |tid0000000000003E08|
     
    () 1 % thread::send tid0000000000003078 {join [set auto_path] \n}
    //zipfs:/lib/tcl/tcl_library
    //zipfs:/lib/tcl
    C:/bawTcl/lib
    C:/bawTcl/lib/cawt
    C:/bawTcl/lib/tcl3d
    C:/bawTcl/lib/tcllib
    C:/bawTcl/lib/tclvfs/template
    //zipfs:/lib/tk/tk_library
    //zipfs:/lib/tk/tk_library/ttk
    
    () 2 % thread::send tid0000000000002F60 {join [set auto_path] \n}
    //zipfs:/lib/tcl/tcl_library
    //zipfs:/lib/tcl
    C:/bawTcl/lib
    C:/bawTcl/lib/cawt
    C:/bawTcl/lib/tcl3d
    C:/bawTcl/lib/tcllib
    C:/bawTcl/lib/tclvfs/template
    C:/bawTcl/lib/tcllib/math
    
    (This 2nd thread did not fail, so Tk was not loaded to display the dialog box)
    
    Here is the similar results on a magicsplat distro, which typically takes many more runs to fail, likely because the timing is different - maybe something to do with the use of zipfs in bawt, but not magicsplat.
    
    D:/podcasts/Tcl90/lib/tcl9.0
    D:/podcasts/Tcl90/lib
    D:/podcasts/Tcl90/lib/cawt3.0.0
    D:/podcasts/Tcl90/lib/tcl3d1.0.0
    D:/podcasts/Tcl90/lib/tcllib2.0
    D:/podcasts/Tcl90/lib/vfs1.4.2/template
    D:/podcasts/Tcl90/lib/tk9.0
    D:/podcasts/Tcl90/lib/tk9.0/ttk
                     (taskname1,error)           = |can't find package math| 
                     (taskname1,tid)             = |tid00000000000031DC| 
                     (taskname2,tid)             = |tid0000000000002490| 
                     (taskname3,tid)             = |tid0000000000003B64| 
    () 1 % thread::send tid00000000000031DC {join [set auto_path] \n}
    D:/podcasts/Tcl90/lib/tcl9.0
    D:/podcasts/Tcl90/lib
    D:/podcasts/Tcl90/lib/cawt3.0.0
    D:/podcasts/Tcl90/lib/tcl3d1.0.0
    D:/podcasts/Tcl90/lib/tcllib2.0
    D:/podcasts/Tcl90/lib/vfs1.4.2/template
    D:/podcasts/Tcl90/lib/tk9.0
    D:/podcasts/Tcl90/lib/tk9.0/ttk
    () 2 % thread::send tid0000000000002490 {join [set auto_path] \n}
    D:/podcasts/Tcl90/lib/tcl9.0
    D:/podcasts/Tcl90/lib
    D:/podcasts/Tcl90/lib/cawt3.0.0
    D:/podcasts/Tcl90/lib/tcl3d1.0.0
    D:/podcasts/Tcl90/lib/tcllib2.0
    D:/podcasts/Tcl90/lib/vfs1.4.2/template
    D:/podcasts/Tcl90/lib/tcllib2.0/math
    () 3 % 
    
    Here is the windows batch script I use to repeatedly run the program. It takes 1 argument, and which decides which of my 4 distributions. You will need to adjust to your system to use it.
    
    For me, the 4 distro's are in order 1-4,
     
    a bawt 9.0.1, 
    magicsplat 9.0.1, 
    a bawt tclkit in a starpack, 
    magicsplat 8.6
    
    It never fails with the 8.6 magicsplat distro. With the bawt tclkit, it actually crashes with an access violation, and the program address is always the same. Unfortunately, I don't have any symbols to use for more info.
    
    
    
    bugx.bat script
    #######################################################
    
    
    @echo off
    setlocal
    
    set "loopCounter=0"
    set "distro=%1" &REM Get the distro parameter
    
    if "%distro%"=="1" (
        set "commandToRun=C:\\bawTcl\\bin\\wish90.exe A:\\bug.tcl"
    ) else if "%distro%"=="2" (
        set "commandToRun=D:\\podcasts\\Tcl90\\bin\\wish90.exe A:\\bug.tcl"
    ) else if "%distro%"=="3" (
        set "commandToRun=C:\\tclf\\kits\\mytcl901.exe A:\\bug.tcl"
    ) else if "%distro%"=="4" (
        set "commandToRun=C:\\Users\\core5\\AppData\\Local\\Apps\\Tcl86\\bin\\wish.exe  A:\\bug.tcl"
    ) else (
        echo Invalid distro parameter.  Please specify 1, 2, 3, or 4.
        exit /b 1
    )
    echo Command to run: %commandToRun%
    :loopStart
    echo Loop Counter: %loopCounter%
    %commandToRun%
    rem timeout /t 1 >nul  &REM Add a 1-second delay
    set /a loopCounter+=1
    goto loopStart
    
    endlocal
    
    
    
    bug.tcl
    #################################################################
    
    console show
    wm withdraw .   
    package require Thread
        
    tsv::set  tids [thread::id] mainthread  ;# for reverse lookup 
    tsv::set  main mainthread [thread::id]  ;# for reverse lookup 
    ################################################# Tasks version 1.13h
    namespace eval tasks {  
    
    proc wait { ms } {              ;# non busy wait
        set uniq [incr ::__sleep__tmp__counter]
        set ::__sleep__tmp__$uniq 0
        after $ms set ::__sleep__tmp__$uniq 1
        vwait ::__sleep__tmp__$uniq
        unset ::__sleep__tmp__$uniq
    }
    
        
    ################################################# dump all task shared variables
    proc tdump {{pat .*} {max 90}} {         ;# dump all the shared Task variables
        set all 1
        set doputz 1
        set out {}
        if { [string index $pat 0] eq "-" } { ;# a leading - reduces output to just the variables
            set all 0
            set pat [string range $pat 1 end]
        } elseif { [string index $pat 0] eq "+" } { ;# a leading + no output puts either AND return results in $out
            set all 0
            set doputz 0
            set pat [string range $pat 1 end]
        }
        if { $all } {
            puts "\n------ Task(s) dump -----------------------------------------"
            puts "tsv::names  = |[tsv::names *]|"
            puts "tsv::tids   = |[tsv::array names tids *]|"
            puts "---------------------------------------------------------------"
        }
        set tvarnames [lsort -stride 2 -index 1 [tsv::array get tids]]
        
        if { $all } {
            puts "tid/names   = |$tvarnames|"
            puts "---------------------------------------------------------------"
        }
        foreach {var val}  [lsort -dictionary -stride 2 -index 1 $tvarnames ] {
            if { $all } {
                puts "[format %-10s $val] tid: $var  exists: [thread::exists $var]"
            }
            
            set tidnames [tsv::array names tvar $val,*]
            foreach tname [lsort $tidnames] {
                if { $tname eq "$val,queue" } {
                    set value [::tsv::lrange tvar $tname 0 end] ;# tsv::get can cause a crash now in threads 2.8.8, so use this way
                } else {
                    set value [tsv::get tvar $tname]
                }
                set value [string map {\n \u2936 \t \u02eb} $value]
                if { [regexp .*${pat}.* "$tname\t[string range $value 0 $max]"] } {
                    if { $doputz } {
                        puts "                 [format %-27s ($tname)] = |[string range $value 0 $max]| "
                    } else {
                        lappend out [list $tname $value]
                    }
                }
            }
        }
        if { $all } {
            puts "---------------------------------------------------------------"
        }
        return $out ;# will be null unless +pat was used - to avoid dummping it all in interactive mode or windows console
    }
    #proc - main Task procs         -----------------------------------------------------------
    #################################################
    proc Task {name args} {        ;# create a Task
    
    set script {
        if [catch {
            package require math
            if [catch {while 1 {
                thread::wait
                
            }} thread_err_code thread_err_dict] {
                tsv::set tvar taskname1,error $thread_err_dict  
                catch {package require Tk}; package require Tk; wm withdraw . ; set zzz [tk_messageBox -type yesno -detail {Select yes to exit, no to suspend} -title {task error} -message "$thread_err_code\n\n$thread_err_dict" -title "tid [thread::id]" ]; 
                if {$zzz eq "yes"} exit else {vwait ::forever1}
            }
        } err_code_Task_Create err_code_Task_Create_details] { 
                tsv::set tvar taskname1,error $err_code_Task_Create
                catch {package require Tk}; package require Tk; wm withdraw . ; set zzz [tk_messageBox -type yesno -detail {Select yes to exit, no to suspend} -title {Task create error} -message "$err_code_Task_Create \n$err_code_Task_Create_details"]
                if {$zzz eq "yes"} exit else {vwait ::forever2}
        }
    }
    
        
        set tid [thread::create $script]
        
        tsv::set  tvar $name,tid        $tid
        tsv::set  tvar $name,script     $script
        tsv::set  tids $tid             $name       ;# for reverse lookup
        return $tid
    }
    
                
    namespace export tdump
    
    }
    # end of tasks namespace eval   
    
        puts [join [set auto_path] \n]
        namespace import tasks::*   
        update
        
        
        tasks::Task taskname1 
        tasks::Task taskname2 
        if { 1 } { ;# when 0, it does not fail on my systems
    #       tasks::wait 10 ;# also sufficient to no longer fail
            tasks::Task taskname3
        }
        
        
        set ok 1
        tasks::wait 100
        tasks::tdump -,error|,tid
        
        foreach t [tasks::tdump +,error] {
            lassign $t a b;
            if {$b ne ""} {bell;set ok 0}
        }
        if { $ok } {
            wm withdraw .
            puts "*****ok******* [info nameof]\n[info patch]"
            after 250 exit
        }
    
  5. foundin changed to: "9.0.1"
  6. is_private changed to: "0"
  7. login: "anonymous"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "anonymous"
  13. subsystem changed to: "39. Package Manager"
  14. title changed to:
    likely race condition with package require in several threads causing error
    
  15. type changed to: "Bug"