Tcl Source Code

View Ticket
Login
Ticket UUID: 3408127
Title: several thread-7.* tests leak
Type: Bug Created on: 2011-09-12 16:05:44
Submitter: dgp Assigned to: vasiljevic
Subsystem: 49. Threading Severity:
Priority: 8 Last modified: 2026-10-22 17:19:24
Status: Closed Closed by: serhiy.storchaka
Resolution: Fixed Closed on: 2026-10-12 17:59:57
Version: obsolete: 8.5.12
Description:
The following tests leak memory:

thread-7.24
thread-7.25
thread-7.26
thread-7.28
thread-7.29
thread-7.30
thread-7.31

The cause is the same in all of them.  The tests make
use of the [testthread exit] command both directly and
indirectly via the [tcltest::threadReap] command.  This
command unavoidably causes leaks.  For this reason
the corresponding Thread command [thread::exit] is
declared deprecated.

I've successfully converted many tests from [testthread]
to the Thread package, making use of the [thread::preserve]
and [thread::release] commands to manage thread lifetimes
without leaking memory.  I'm not able to convert the tests
above though.

These tests are testing Tcl_CancelEval via both the
[interp cancel] command and a [testthread cancel] testing
command. I've tried to convert these, but I cause 
intermittent segfaults with my attempts.  My hope is
that my failures are due to lack of experience with
cancel, and the matter will be easy for those who know it.
User Comments:
dgp added on 2011-09-12 23:08:41:
BTW, the fact that I can trigger segfaults
with scripts, even by accident, suggests
there's some pretty serious bugs in either
the *Cancel* machinery or the Thread package
or both or their combination.

dgp added on 2011-11-18 03:42:55:
I've been working on converting the
thread-7.* tests from using the [testthread]
command to the Thread package.  At the
same time, I've been trying to eliminate
the empirical use of [after] delays, and put
in place whatever sync is needed to make
the tests robust.

I've got problems.

First, I haven't been able to come
up with a solution when the object
is to test that some script is not
cancelled.  For example thread-7.18.

Second, I've come to discover that when
I removed some [after]s, even the passing
tests are not actually demonstrating successul
cancel of the code intended to get canceled.
Often, the cancel is happening while the
thread::send is still in progress.

I need help with this.

mistachkin added on 2011-11-18 08:54:55:
Fixing these issues requires a couple more minor enhancements to the Thread package.  I'm working on them now.

mistachkin added on 2011-11-18 12:06:39:
All of these tests have been fixed.  Please verify and close this bug.  If there are any additional issues, please let me know.

mistachkin added on 2011-11-18 12:07:29:
Also, note that you need the very latest version of the Thread package (currently the tip of the thread-2-7-branch branch) in order for all the tests to pass.

mistachkin added on 2011-11-18 14:28:15:
Careful analysis of ThreadEventProc in threadCmd.c reveals that in some cases it is neglecting to Tcl_Release the Tcl interp inside eventPtr->clbkData.  I'm still trying to fully understand all possible paths through this function in my head.

dgp added on 2012-07-13 01:04:42:
Here's the ticket where the troublesome tests have
been worked on.  Here's output testing the 8.6b3
RC, all trimmed away except the interesting bits:

---- thread-7.26 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.26 PASSED
...
---- thread-7.28 start


==== thread-7.28 cancel: send async cancel nested catch inside pure bytecode loop FAILED
==== Contents of test case:

    unset -nocomplain ::threadSawError ::threadError ::threadId ::threadIdStarted
    set serverthread [thread::create -joinable  [string map [list %ID% [thread::id]] {
        proc foobar {} {
            while {1} {
                if {![info exists foo]} then {
                    # signal the primary thread that we are ready
                    # to be canceled now (we are running).
                    thread::send %ID% [list set ::threadIdStarted [thread::id]]
                    set foo 1
                }
                catch {
                    while {1} {
                        catch {
                            while {1} {
                                # we must call update here because otherwise
                                # the thread cannot even be forced to exit.
                                update
                            }
                        }
                    }
                }
            }
        }
        foobar
    }]]
    # wait for other thread to signal "ready to cancel"
    vwait ::threadIdStarted; after 1000
    set res [thread::send -async $serverthread {interp cancel}]
    thread::send $serverthread $::threadSuperKillScript
    vwait ::threadSawError($serverthread)
    thread::join $serverthread; drainEventQueue
    list $res [expr {[info exists ::threadIdStarted] ?  $::threadIdStarted == $serverthread : 0}]  [expr {[info exists ::threadId] ?  $::threadId == $serverthread : 0}]  [expr {[info exists ::threadError($serverthread)] ?  [findThreadError $::threadError($serverthread)] : ""}]

---- Test generated error; Return code was: 1
---- Return code should have been one of: 0 2
---- errorInfo: target thread died
    while executing
"thread::send $serverthread $::threadSuperKillScript"
    ("uplevel" body line 30)
    invoked from within
"uplevel 1 $script"
---- errorCode: NONE
==== thread-7.28 FAILED


---- thread-7.29 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.29 PASSED
---- thread-7.30 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.30 PASSED
---- thread-7.31 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.31 PASSED
...
---- thread-7.34 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.34 PASSED
---- thread-7.35 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.35 PASSED
---- thread-7.36 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.36 PASSED
---- thread-7.37 start
WARNING: drained 1 event(s) on main thread
++++ thread-7.37 PASSED

The WARNINGs are reportedly evidence of pthread
misbehavior.  The single failure in this run is only one
possible outcome.  As few as zero and as many as 4
test failures are common possibilities.

ferrieux added on 2012-07-13 01:11:31:
Can someone write a comment here, explaining what libpthread is supposed to be doing wrong ?

mistachkin added on 2012-07-13 01:22:05:
It's actually hard to pin down exactly what pthreads is doing incorrectly.  If I remember correctly, it was "losing" events in some cases.  There is also an issue with spurious events; however, I think that was related to the Tcl event loop processing code.  Another question: Are the actual leaks originally reported in this bug gone now?

ferrieux added on 2012-07-13 01:58:39:
Sorry, I lack the bandwidth to reconsider the whole bug ecosystem. I am not a user of Tcl threads, my experience is with libpthread in other (non-Tcl) contexts, hence my offer to help. I'm still eager to help if the spotlight focuses on libpthread at some point, but reassigning to Zoran in the meantime.

dgp added on 2012-07-13 03:00:52:
I should find time to run valgrind again to be certain,
but my recollection is that thread.test is now leak free.

It's noisy and failure-prone, but it doesn't leak memory.

mistachkin added on 2012-07-13 10:09:46:
These test failures do not appear to reproduce on core.tcl.tk, nor do they reproduce on Windows using the trunk of Tcl and the thread-2-7-branch branch of the Thread package.

dgp added on 2012-07-16 20:21:24:
Thanks for checking.  I will find time to see whether I
can better demonstrate the issues, or figure out what
the trouble is.

If I cannot succeed in communicating to you the problems
needing solving, then I'm inclined to just revert as much of
thread.test as necessary to what "worked" before in the interest
of getting 8.6b3 done.

ferrieux added on 2012-07-16 20:58:35:
Wondering why this gets repeatedly reassigned to me ... I'm handing it back to Zoran because he's (1) the obvious expert and (2) got some free cycles for Tcl these days (or so it seems).

dgp added on 2012-07-16 21:01:22:
Apologies ferrieux.  That looks like a SF Tracker bug
that I'm triggering.  I keep this ticket open in a Browser
tab, and although I get comment updates via some
browser magic, my form data values remain the same,
so whenever I submit a comment, my outdated values
get posted too.

stwo added on 2012-07-16 21:38:59:
All tests pass on OpenBSD-current using the trunk of Tcl and the thread-2-7-branch branch of the Thread package.

dgp added on 2012-07-16 21:47:23:
Be sure to check before claiming "All tests pass".

For at least some folks having no trouble with thread.test,
it's not because "All tests pass" but because "All tests skipped".

In particular, a working, [package require]-able Thread 2.7 has
to be around for the troublesome tests to even be attempted.

stwo added on 2012-07-16 23:05:06:
Latest greatest Tcl and Thread 2.7 on OpenBsd-amd64-current:

Tests began at Mon Jul 16 12:02:23 EDT 2012
thread.test
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread
WARNING: drained 1 event(s) on main thread

Tests ended at Mon Jul 16 12:02:38 EDT 2012
all.tcl:        Total   51      Passed  49      Skipped 2       Failed  0
Sourced 1 Test Files.
Number of tests skipped for each constraint:
        2       knownBug

dgp added on 2012-07-16 23:27:50:
Thanks.  Now I want the WARNINGs to be gone.
It's easy to silence them, but AIUI, mistachkin
says their presence indicates malfunctioning he's 
having to work around.  I don't want to kill the canaries
if they're telling us something useful.

Also, although I see runs like that one some of the time,
I also see test failures.  These thread tests require several
runs to see their whole spectrum of behaviors.

nijtmans added on 2012-07-17 03:18:22:
Tried the same on UBUNTU amd64, and got
the same 9 messsages, but no failures, even
after serveral runs.

How about silencing those messages in the
8.6b3-rc branch, but not on trunk? That
still gives some time to fix the 9 involved
test-cases, but it doesn't need to hold
the b3 release. (of course, when merging
back the rc branch to trunk, this silencing
must NOT be merged back!)

---- thread-7.26 start
WARNING: drained 1 event(s) on main thread
---- thread-7.27 start
---- thread-7.28 start
WARNING: drained 1 event(s) on main thread
---- thread-7.29 start
WARNING: drained 1 event(s) on main thread
---- thread-7.30 start
WARNING: drained 1 event(s) on main thread
---- thread-7.31 start
WARNING: drained 1 event(s) on main thread
---- thread-7.32 start
---- thread-7.33 start
---- thread-7.34 start
WARNING: drained 1 event(s) on main thread
---- thread-7.35 start
WARNING: drained 1 event(s) on main thread
---- thread-7.36 start
WARNING: drained 1 event(s) on main thread
---- thread-7.37 start
WARNING: drained 1 event(s) on main thread

Tests ended at Mon Jul 16 22:02:36 CEST 2012
all.tcl:Total51Passed49Skipped2Failed0

serhiy.storchaka added on 2026-10-12 17:59:57:

The thread-7.* tests no longer use testthread exit, and with 9.1 and Thread 3.0.7 they pass in 10 runs out of 10. Valgrind shows no leaks in Tcl. The only remaining leak (12 bytes) is in the Thread package: the result copied by ThreadSetResult() is sometimes not freed when the script is cancelled.