Skip to content
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
35 changes: 35 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,41 @@ def __del__(self):
self.assertEqual(out.strip(), b"OK")
self.assertIn(b"can't create new thread at interpreter shutdown", err)

def test_start_new_thread_failed(self):
# gh-109746: if Python fails to start newly created thread
# due to failure of underlying PyThread_start_new_thread() call,
# its state should be removed from interpreter' thread states list
# to avoid its double cleanup
try:
from resource import setrlimit, RLIMIT_NPROC
except ImportError as err:
self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
code = """if 1:
import resource
import _thread

def f():
print("shouldn't be printed")

limits = resource.getrlimit(resource.RLIMIT_NPROC)
[_, hard] = limits
resource.setrlimit(resource.RLIMIT_NPROC, (0, hard))

try:
_thread.start_new_thread(f, ())
except RuntimeError:
print('ok')
else:
print('skip')
"""
_, out, err = assert_python_ok("-u", "-c", code)
out = out.strip()
if out == b'skip':
self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
self.assertEqual(out, b'ok')
self.assertEqual(err, b'')


class ThreadJoinOnShutdown(BaseTestCase):

def _run_and_join(self, script):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If :func:`!_thread.start_new_thread` fails to start a new thread, it deletes its state from interpreter and thus avoids its repeated cleanup on finalization.
1 change: 1 addition & 0 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
if (ident == PYTHREAD_INVALID_THREAD_ID) {
PyErr_SetString(ThreadError, "can't start new thread");
PyThreadState_Clear(boot->tstate);
PyThreadState_Delete(boot->tstate);
thread_bootstate_free(boot, 1);
return NULL;
}
Expand Down
4 changes: 3 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,9 @@ tstate_delete_common(PyThreadState *tstate)
if (tstate->_status.bound_gilstate) {
unbind_gilstate_tstate(tstate);
}
unbind_tstate(tstate);
if (tstate->_status.bound) {
unbind_tstate(tstate);
}

// XXX Move to PyThreadState_Clear()?
clear_datastack(tstate);
Expand Down
Loading