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

Skip to content

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

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

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 21 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,27 @@ 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()
# Thread.join() is not called

def test_leak_without_join_2(self):
# Same as above, but a delay gets introduced after the thread's
# Python code returned but before the thread state is deleted.
def random_sleep():
seconds = random.random() * 0.010
time.sleep(seconds)

def f():
random_sleep()

with threading_helper.wait_threads_exit():
threading.Thread(target=f).start()
# Thread.join() is not called


class ThreadJoinOnShutdown(BaseTestCase):
Expand Down
9 changes: 9 additions & 0 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,15 @@ def _register_atexit(func, *arg, **kwargs):

_main_thread = _MainThread()

def _discard_shutdown_lock(lock):
"""
Discard the Thread._tstate_lock lock when the non-daemon thread have done.
"""
if lock not in _shutdown_locks:
return
with _shutdown_locks_lock:
_shutdown_locks.discard(lock)

def _shutdown():
"""
Wait until the Python thread state of all non-daemon threads get deleted.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a reference leak if a thread is not joined.
25 changes: 24 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
/* Interface to Sjoerd's portable C thread library */

#include "Python.h"
#include "pycore_pylifecycle.h"
#include "pycore_interp.h" // _PyInterpreterState.num_threads
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h" // _PyThreadState_Init()
#include <stddef.h> // offsetof()
#include "structmember.h" // PyMemberDef
Expand All @@ -20,6 +21,8 @@ _Py_IDENTIFIER(__dict__);

_Py_IDENTIFIER(stderr);
_Py_IDENTIFIER(flush);
_Py_IDENTIFIER(threading);
_Py_IDENTIFIER(_discard_shutdown_lock);


// Forward declarations
Expand Down Expand Up @@ -1283,13 +1286,33 @@ release_sentinel(void *wr_raw)
execute here. */
PyObject *obj = PyWeakref_GET_OBJECT(wr);
lockobject *lock;

if (obj != Py_None) {
lock = (lockobject *) obj;
if (lock->locked) {
PyThread_release_lock(lock->lock_lock);
lock->locked = 0;
}
PyThreadState *tstate = _PyThreadState_GET();
PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
if (threading == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetString(tstate, PyExc_RuntimeError,
"lost threading module");
}
goto exit;
}
PyObject *result = _PyObject_CallMethodIdOneArg(
threading, &PyId__discard_shutdown_lock, obj);
if (result == NULL) {
goto exit;
} else {
Py_DECREF(result);
}
Py_DECREF(threading);
}

exit:< 498D /td>
/* Deallocating a weakref with a NULL callback only calls
PyObject_GC_Del(), which can't call any Python code. */
Py_DECREF(wr);
Expand Down
0