8000 bpo-37788: Fix reference leak when Thread is never joined by pitrou · Pull Request #26103 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37788: Fix reference leak when Thread is never joined #26103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
7 changes: 7 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,13 @@ def __call__(self):
thread.join()
self.assertTrue(target.ran)

def test_leak_without_join(self):
# bpo-37788: Test that a thread which is not joined explicitly
# does not leak. Test written for reference leak checks.
def noop(): pass
with threading_helper.wait_threads_exit():
threading.Thread(target=noop).start()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to add a test case that the target function isn't just only the pass

8000 Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what do you mean exactly? I'm not sure I understand your suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. I didn't explain it clearly. I suggest that the target of Thread could be more complex ;)

Something like:

def noop(): 
      # we don't what code will run in the thread in fact.
      time.sleep(random.random())
with threading_helper.wait_threads_exit():
      threading.Thread(target=noop).start()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what would that change? What matters is what happens after the thread ends.

Copy link
Member
8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, give me a second.
No matter how complex of the target of thread, the thread will finish in time in python level. So we don't adding a complex testcase in here is fine. right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly.

# Thread.join() is not called


class ThreadJoinOnShutdown(BaseTestCase):
Expand Down
19 changes: 18 additions & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,12 +780,27 @@ def _newname(name_template):
_active = {} # maps thread id to Thread object
_limbo = {}
_dangling = WeakSet()

# Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown()
# to wait until all Python thread states get deleted:
# see Thread._set_tstate_lock().
_shutdown_locks_lock = _allocate_lock()
_shutdown_locks = set()

def _maintain_shutdown_locks():
"""
Drop any shutdown locks that don't correspond to running threads anymore.

Calling this from time to time avoids an ever-growing _shutdown_locks
set when Thread objects are not joined explicitly. See bpo-37788.

This must be called with _shutdown_locks_lock acquired.
"""
# If a lock was released, the corresponding thread has exited
to_remove = [lock for lock in _shutdown_locks if not lock.locked()]
_shutdown_locks.difference_update(to_remove)


# Main class for threads

class Thread:
Expand Down Expand Up @@ -968,6 +983,7 @@ def _set_tstate_lock(self):

if not self.daemon:
with _shutdown_locks_lock:
_maintain_shutdown_locks()
_shutdown_locks.add(self._tstate_lock)

def _bootstrap_inner(self):
Expand Down Expand Up @@ -1023,7 +1039,8 @@ def _stop(self):
self._tstate_lock = None
if not self.daemon:
with _shutdown_locks_lock:
_shutdown_locks.discard(lock)
# Remove our lock and other released locks from _shutdown_locks
_maintain_shutdown_locks()

def _delete(self):
"Remove current thread from the dict of currently running threads."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a reference leak when a Thread object is never joined.
0