Repository navigation
Closed
Labels
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifeperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directory
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = <Date 2021-05-15.09:53:03.268>
created_at = <Date 2019-08-07.16:22:52.588>
labels = ['3.11', 'library', '3.9', '3.10', 'performance']
title = 'fix for bpo-36402 (threading._shutdown() race condition) causes reference leak'
updated_at = <Date 2021-09-08.12:30:51.049>
user = 'https://github.com/akruis'bugs.python.org fields:
activity = <Date 2021-09-08.12:30:51.049>
actor = 'victorvorobev'
assignee = 'none'
closed = True
closed_date = <Date 2021-05-15.09:53:03.268>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2019-08-07.16:22:52.588>
creator = 'anselm.kruis'
dependencies = []
files = ['48534']
hgrepos = []
issue_num = 37788
keywords = ['patch', '3.7regression']
message_count = 23.0
messages = ['349173', '349225', '349973', '349977', '349980', '354460', '354463', '358641', '359477', '359479', '371866', '380391', '388606', '388609', '388611', '388618', '389671', '389696', '390118', '390817', '393709', '393710', '394307']
nosy_count = 16.0
nosy_names = ['mark.dickinson', 'jaraco', 'pitrou', 'vstinner', 'python-dev', 'martin.panter', 'koobs', 'anselm.kruis', 'pablogsal', 'miss-islington', 'shihai1991', 'Zhipeng Xie', 'krypticus', 'Arkady M', 'ViktorPegy', 'victorvorobev']
pr_nums = ['15175', '15228', '15336', '15337', '15338', '25148', '25226', '26103', '26138', '26142', '27473', '28239']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'resource usage'
url = 'https://bugs.python.org/issue37788'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifeperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directory
Activity
akruis commentedon Aug 7, 2019
Starting with commit 468e5fe (bpo-36402: Fix threading._shutdown() race condition (GH-13948)) the following trivial test case leaks one reference and one memory block.
Attached is a patch, that adds this test case to Lib/test/test_threading.py. After you apply this patch "python -m test -R: test_threading" leaks one (additional) reference. This leak is also present in Python 3.7.4 and 3.8.
I'm not sure, if it correct not to join a thread, but it did work flawlessly and didn't leak in previous releases.
I didn't analyse the root cause yet.
akruis commentedon Aug 8, 2019
The root cause for the reference leak is the global set threading._shutdown_locks. It contains Thread._tstate_lock locks of non-daemon threads. If a non-daemon thread terminates and no other thread joins the terminated thread, the _tstate_lock remains in threading._shutdown_locks forever.
I could imagine that a long running server could accumulate many locks in threading._shutdown_locks over time. Therefore the leak should be fixed.
There are probably several ways to deal with this issue. A straight forward approach is to discard the lock from within
tstate->on_deletehook, that is function "void release_sentinel(void *)" in _threadmodule.c. Pull request (GH-15175) implements this idea. Eventually I should add another C-Python specific test-case to the PR.vstinner commentedon Aug 19, 2019
New changeset d3dcc92 by Victor Stinner in branch 'master':
bpo-37788: Fix a reference leak if a thread is not joined (GH-15228)
d3dcc92
vstinner commentedon Aug 19, 2019
This change introduced a regression :-(
#15228 (comment)
I created PR 15338 to revert it
vstinner commentedon Aug 19, 2019
New changeset d11c2c6 by Victor Stinner in branch 'master':
Revert "bpo-37788: Fix a reference leak if a thread is not joined (GH-15228)" (GH-15338)
d11c2c6
vstinner commentedon Oct 11, 2019
Copy of wmanley's comment:
#13948 (comment)
"""
This caused a regression for people overriding Thread.join to implement custom thread interruption. See https://stackoverflow.com/questions/50486083/ending-non-daemon-threads-when-shutting-down-an-interactive-python-session
"""
pablogsal commentedon Oct 11, 2019
Hummm....is this really a regression? The docs say:
So if someone is overriding join() they are out of contract
krypticus commentedon Dec 18, 2019
I ran into this bug as well, and opened an issue for it (before I saw this issue): https://bugs.python.org/issue39074
Was there a conclusion on the best way to fix this? It seems like the previous __del__ implementation would correct the resource leakage by removing the _tstate_lock from _shutdown_locks.
vstinner commentedon Jan 6, 2020
I marked bpo-39074 as a duplicate of this issue.
vstinner commentedon Jan 6, 2020
I marked bpo-39201 as a duplicate of this issue.
6 remaining items
mdickinson commentedon Mar 13, 2021
Re-opening; I believe this issue is still valid.
Here's output on a debug build on current master (commit 7591d94) on my machine:
vstinner commentedon Mar 13, 2021
Oh, I'm confused. This issue is about the threading module, whereas bpo-37193 is unrelated: it's about the socketserver module.
haoren3696 commentedon Mar 29, 2021
ping
I also encountered this problem. Is there a fix now?
jaraco commentedon Mar 29, 2021
No. The issue remains open.
vadmium commentedon Apr 3, 2021
Anselm's pull request PR 15175 looked hopeful to me. I've been using those changes in our builds at work for a while.
shihai1991 commentedon Apr 12, 2021
I created PR 25226. It's a another way to solve this problem.
Compared to PR 15175,
_set_sentinel()don't need to receive any params :)miss-islington commentedon May 15, 2021
New changeset 71dca6e by Miss Islington (bot) in branch '3.10':
[3.10] bpo-37788: Fix reference leak when Thread is never joined (GH-26103) (GH-26138)
71dca6e
miss-islington commentedon May 15, 2021
New changeset b30b25b by Antoine Pitrou in branch '3.9':
[3.9] bpo-37788: Fix reference leak when Thread is never joined (GH-26103) (GH-26142)
b30b25b
vstinner commentedon May 25, 2021
Fix in the main branch:
commit c10c2ec
Author: Antoine Pitrou <[email protected]>
Date: Fri May 14 21:37:20 2021 +0200