You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Threading memory leak in _shutdown_locks for non-daemon threads #83255
When running 3.7, we noticed a memory leak in threading._shutdown_locks when non-deamon threads are started but "join()" or "is_alive()" is never called. Here's a test to illustrate the growth:
New Bytes: 840 Total Bytes 840 New blocks: 1 Total blocks: 1:
File "/usr/local/lib/python3.6/threading.py", line 884
self._bootstrap_inner()
New Bytes: 608 Total Bytes 608 New blocks: 4 Total blocks: 4:
File "/usr/local/lib/python3.6/tracemalloc.py", line 387
self.traces = _Traces(traces)
File "/usr/local/lib/python3.6/tracemalloc.py", line 524
return Snapshot(traces, traceback_limit)
File "/gems/tests/integration/endpoint_connection_test.py", line 856
snap1 = tracemalloc.take_snapshot()
File "/usr/local/lib/python3.6/site-packages/_pytest/python.py", line 198
testfunction(**testargs)
File "/usr/local/lib/python3.6/site-packages/pluggy/callers.py", line 187
res = hook_impl.function(*args)
File "/usr/local/lib/python3.6/site-packages/pluggy/manager.py", line 87
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "/usr/local/lib/python3.6/site-packages/pluggy/manager.py", line 93
return self._inner_hookexec(hook, methods, kwargs)
File "/usr/local/lib/python3.6/site-packages/pluggy/hooks.py", line 286
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "/usr/local/lib/python3.6/site-packages/_pytest/python.py", line 1459
self.ihook.pytest_pyfunc_call(pyfuncitem=self)
File "/usr/local/lib/python3.6/site-packages/_pytest/runner.py", line 111
item.runtest()
==========
Output in v3.7.4:
New Bytes: 36000 Total Bytes 36000 New blocks: 1000 Total blocks: 1000:
File "/usr/local/lib/python3.7/threading.py", line 890
self._bootstrap_inner()
File "/usr/local/lib/python3.7/threading.py", line 914
self._set_tstate_lock()
File "/usr/local/lib/python3.7/threading.py", line 904
self._tstate_lock = _set_sentinel()
New Bytes: 32768 Total Bytes 32768 New blocks: 1 Total blocks: 1:
File "/usr/local/lib/python3.7/threading.py", line 890
self._bootstrap_inner()
File "/usr/local/lib/python3.7/threading.py", line 914
self._set_tstate_lock()
File "/usr/local/lib/python3.7/threading.py", line 909
_shutdown_locks.add(self._tstate_lock)
=================
It looks like this commit didn't take into account the tstate_lock cleanup that happens in the C code, and it's not removing the _tstate_lock of completed threads from the _shutdown_locks once the thread finishes, unless the code manually calls "join()" or "is_alive()" on the thread:
Activity
krypticus commentedon Dec 17, 2019
When running 3.7, we noticed a memory leak in threading._shutdown_locks when non-deamon threads are started but "join()" or "is_alive()" is never called. Here's a test to illustrate the growth:
=========
=========
Output in v3.6.8:
New Bytes: 840 Total Bytes 840 New blocks: 1 Total blocks: 1:
File "/usr/local/lib/python3.6/threading.py", line 884
self._bootstrap_inner()
New Bytes: 608 Total Bytes 608 New blocks: 4 Total blocks: 4:
File "/usr/local/lib/python3.6/tracemalloc.py", line 387
self.traces = _Traces(traces)
File "/usr/local/lib/python3.6/tracemalloc.py", line 524
return Snapshot(traces, traceback_limit)
File "/gems/tests/integration/endpoint_connection_test.py", line 856
snap1 = tracemalloc.take_snapshot()
File "/usr/local/lib/python3.6/site-packages/_pytest/python.py", line 198
testfunction(**testargs)
File "/usr/local/lib/python3.6/site-packages/pluggy/callers.py", line 187
res = hook_impl.function(*args)
File "/usr/local/lib/python3.6/site-packages/pluggy/manager.py", line 87
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "/usr/local/lib/python3.6/site-packages/pluggy/manager.py", line 93
return self._inner_hookexec(hook, methods, kwargs)
File "/usr/local/lib/python3.6/site-packages/pluggy/hooks.py", line 286
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "/usr/local/lib/python3.6/site-packages/_pytest/python.py", line 1459
self.ihook.pytest_pyfunc_call(pyfuncitem=self)
File "/usr/local/lib/python3.6/site-packages/_pytest/runner.py", line 111
item.runtest()
==========
Output in v3.7.4:
New Bytes: 36000 Total Bytes 36000 New blocks: 1000 Total blocks: 1000:
File "/usr/local/lib/python3.7/threading.py", line 890
self._bootstrap_inner()
File "/usr/local/lib/python3.7/threading.py", line 914
self._set_tstate_lock()
File "/usr/local/lib/python3.7/threading.py", line 904
self._tstate_lock = _set_sentinel()
New Bytes: 32768 Total Bytes 32768 New blocks: 1 Total blocks: 1:
File "/usr/local/lib/python3.7/threading.py", line 890
self._bootstrap_inner()
File "/usr/local/lib/python3.7/threading.py", line 914
self._set_tstate_lock()
File "/usr/local/lib/python3.7/threading.py", line 909
_shutdown_locks.add(self._tstate_lock)
=================
It looks like this commit didn't take into account the tstate_lock cleanup that happens in the C code, and it's not removing the _tstate_lock of completed threads from the _shutdown_locks once the thread finishes, unless the code manually calls "join()" or "is_alive()" on the thread:
468e5fe
Let me know if I can provide more clarity on this!
krypticus commentedon Dec 17, 2019
Looks like this issue might be a duplicate of https://bugs.python.org/issue37788
vstinner commentedon Jan 6, 2020
Ok, I close this issue as a duplicate of bpo-37788.