[ python-Bugs-1703448 ] "t.join(); assert t not in threading.enumerate()" fails

SourceForge.net noreply at sourceforge.net
Thu Apr 19 10:22:36 CEST 2007


Bugs item #1703448, was opened at 2007-04-19 18:22
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1703448&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Andrew Bennetts (spiv)
Assigned to: Nobody/Anonymous (nobody)
Summary: "t.join(); assert t not in threading.enumerate()" fails

Initial Comment:
This script always fails for me, usually on the very first iteration:

----
import threading

while True:
    print '.',
    t = threading.Thread(target=lambda: None)
    t.start()
    t.join()
    alive_threads = threading.enumerate()
    assert len(alive_threads) == 1, alive_threads
----

The docs clearly say this is expected to work: Thread.join is described as "Wait until the thread terminates" and threading.enumerate's description says it will "Return a list of all currently active Thread objects ... It excludes terminated threads".  So by the time t.join() returns, the thread should not be reported by threading.enumerate anymore.

The bug appears to be that while the thread is shutting down, join() may exit too early: it waits for the __stopped flag to be set, but the thread hasn't necessarily been removed from the _active dict by that time, so enumerate will report a "stopped" thread.  (Or if you like the bug is that __stopped is can be set while the thread is still in the _active dict.)

A workaround is to filter the results of threading.enumerate() through [t for t in threading.enumerate() if t.isAlive()].

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1703448&group_id=5470


More information about the Python-bugs-list mailing list