Skip to content
Tcl Source Code View Ticket
Login

View Ticket

"package require" is not thread-safe

Closed / Duplicate36f6f784370384e866939d56e6c35f57473b8b77Created 1 year agoUpdated 1 year ago
Descriptionplain2025-08-12 19:48:35
The "package require" command fails on Windows when loading the same package in multiple threads simultaneously:

    package require Thread

    set numThreads 100
    for {set i 0} {$i < $numThreads} {incr i} {
        set id [::thread::create]
        ::thread::send -async $id "package require itcl"
        #after 100 ;# uncomment to pass
    }

One possible result is:

    Error from thread tid0000000000009d6c
    can't find package itcl
        while executing
    "package require itcl"
    Error from thread tid0000000000001dc8
    can't find package itcl
        while executing
    "package require itcl"
    Error from thread tid0000000000002c48
    can't find package itcl
        while executing
    "package require itcl"
    Error from thread tid000000000000af44
    can't find package itcl
        while executing
    "package require itcl"

The exact number of errors varies and depends on the speed of the computer and the number of threads spawned. The error does not occur on Linux or in Tcl 8.6.16. The workaround is to add a slight delay between commands, e.g. the "after" command above. The exact delay also depends on the speed of the computer and the number of threads spawned. This can significantly slow down startup time when spawning a large number of threads and does not guarantee that the error will not occur later.
sebreswiki2025-08-12 20:32:27

Duplicate of [61c01e0edb]

Shortly: it was not package require but glob.
Already fixed in all versions (see the ticket).

Log in (anonymous is fine) to add a comment.