8000 bpo-37788: Fix a reference leak if a thread is not joined by vstinner · Pull Request #15228 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37788: Fix a reference leak if a thread is not joined #15228

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
Aug 19, 2019
Merged

bpo-37788: Fix a reference leak if a thread is not joined #15228

merged 1 commit into from
Aug 19, 2019

Conversation

vstinner
Copy link
Member
@vstinner vstinner commented Aug 12, 2019

Add threading.Thread.del() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.

https://bugs.python.org/issue37788

Add threading.Thread.__del__() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.
@vstinner
Copy link
Member Author

@akruis proposed a different approach: his PR #15175 modify the sentinel callback to call something like:

with _shutdown_locks_lock:
    _shutdown_locks.discard(tstate_lock)

@akruis
Copy link
akruis commented Aug 13, 2019

I have two points:

  1. Our fixes are semantically different: Probably the del approach allows for a shutdown race condition, if the thread raises an exception.
  2. Adding __del__ to Thread is surely an option for 3.8 and master, but is it also OK for 3.7.5?

@vstinner
Copy link
Member Author

@pitrou, @pablogsal, @methane, @serhiy-storchaka: my https://bugs.python.org/issue36402 fix introduced a memory leak when a thread is started but never joined explicitly. This PR 8000 adds a destructor to thread.Thread to fix the regression. What do you think? We need to fix 3.7, 3.8 and master branches, I would prefer to use the same fix in all branches.

@akruis proposed a different fix: PR #15175.

Copy link
Member
@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks @vstinner !

@pitrou
Copy link
Member
pitrou commented Aug 19, 2019

@akruis proposed a different fix: PR #15175.

That fix is much more complicated. I prefer this PR.

@serhiy-storchaka
Copy link
Member

__del__ makes an object non-collectable. Is it possible to use weakref.finalize() here?

@pitrou
Copy link
Member
pitrou commented Aug 19, 2019

del makes an object non-collectable

That hasn't been the case in a long time, except if you're talking about Python 2.7.

@serhiy-storchaka
Copy link
Member

So we can now get rid of weakref.finalize() in the tempfile module?

@pitrou
Copy link
Member
pitrou commented Aug 19, 2019

Hmm... probably? I'll let you figure out :-)

@serhiy-storchaka
Copy link
Member

That code was added after 2.7, it was not the first attempt to fix some issues with tempfile, and only it was successful.

@vstinner
Copy link
Member Author

del makes an object non-collectable

That's no longer the case since @pitrou's PEP 442 https://www.python.org/dev/peps/pep-0442/ (finalizers).

@vstinner vstinner merged commit d3dcc92 into python:master Aug 19, 2019
@vstinner vstinner deleted the thread_join_leak branch August 19, 2019 22:37
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7, 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

I'm having trouble backporting to 3.8. Reason: 'Error 110 while writing to socket. Connection timed out.'. Please retry by removing and re-adding the needs backport to 3.8 label.

@bedevere-bot
Copy link

GH-15336 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 19, 2019
…5228)

Add threading.Thread.__del__() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.
(cherry picked from commit d3dcc92)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 19, 2019
…5228)

Add threading.Thread.__del__() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.
(cherry picked from commit d3dcc92)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
@bedevere-bot
Copy link

GH-15337 is a backport of this pull request to the 3.8 branch.

@vstinner
Copy link
Member Author

Oh, my change is wrong: it introduced a regression :-(

vstinner@apu$ ./python -m test -v test_threading -m test_threads_join_2 -F
(...)
0:00:00 load avg: 0.85 [  4] test_threading
test_threads_join_2 (test.test_threading.SubinterpThreadingTests) ... Fatal Python error: Py_EndInterpreter: not the last thread

vstinner added a commit that referenced this pull request Aug 19, 2019
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
…5228)

Add threading.Thread.__del__() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…5228)

Add threading.Thread.__del__() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
…5228)

Add threading.Thread.__del__() method to ensure that the thread state
lock is removed from the _shutdown_locks list when a thread
completes.
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
@harmon
Copy link
harmon commented Sep 24, 2020

Any movement on fixing this leak when threads aren't "join"-ed? I couldn't upgrade to 3.7 because of this leak, and now this leak was backported to 3.6, so I can't upgrade past 3.6.9... (nevermind, I looked at the wrong commit)

Thanks for all your work on Python, I see you've been quick active in making it better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0