Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75ef92d

Browse files
[3.13]gh-109746: Make _thread.start_new_thread delete state of new thread on its startup failure (GH-109761) (GH-127171)
If Python fails to start newly created threaddue to failure of underlying PyThread_start_new_thread() call,its state should be removed from interpreter' thread states listto avoid its double cleanup.(cherry picked from commitca3ea9a)Co-authored-by: Radislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
1 parent950daf8 commit75ef92d

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

‎Lib/test/test_threading.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,41 @@ def __del__(self):
11711171
self.assertEqual(out.strip(),b"OK")
11721172
self.assertIn(b"can't create new thread at interpreter shutdown",err)
11731173

1174+
deftest_start_new_thread_failed(self):
1175+
# gh-109746: if Python fails to start newly created thread
1176+
# due to failure of underlying PyThread_start_new_thread() call,
1177+
# its state should be removed from interpreter' thread states list
1178+
# to avoid its double cleanup
1179+
try:
1180+
fromresourceimportsetrlimit,RLIMIT_NPROC
1181+
exceptImportErroraserr:
1182+
self.skipTest(err)# RLIMIT_NPROC is specific to Linux and BSD
1183+
code="""if 1:
1184+
import resource
1185+
import _thread
1186+
1187+
def f():
1188+
print("shouldn't be printed")
1189+
1190+
limits = resource.getrlimit(resource.RLIMIT_NPROC)
1191+
[_, hard] = limits
1192+
resource.setrlimit(resource.RLIMIT_NPROC, (0, hard))
1193+
1194+
try:
1195+
_thread.start_new_thread(f, ())
1196+
except RuntimeError:
1197+
print('ok')
1198+
else:
1199+
print('skip')
1200+
"""
1201+
_,out,err=assert_python_ok("-u","-c",code)
1202+
out=out.strip()
1203+
ifout==b'skip':
1204+
self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
1205+
self.assertEqual(out,b'ok')
1206+
self.assertEqual(err,b'')
1207+
1208+
11741209
classThreadJoinOnShutdown(BaseTestCase):
11751210

11761211
def_run_and_join(self,script):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

‎Modules/_threadmodule.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
421421
PyThread_handle_tos_handle;
422422
if (PyThread_start_joinable_thread(thread_run,boot,&ident,&os_handle)) {
423423
PyThreadState_Clear(boot->tstate);
424+
PyThreadState_Delete(boot->tstate);
424425
thread_bootstate_free(boot,1);
425426
PyErr_SetString(ThreadError,"can't start new thread");
426427
gotostart_failed;

‎Python/pystate.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,9 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
18111811
if (tstate->_status.bound_gilstate) {
18121812
unbind_gilstate_tstate(tstate);
18131813
}
1814-
unbind_tstate(tstate);
1814+
if (tstate->_status.bound) {
1815+
unbind_tstate(tstate);
1816+
}
18151817

18161818
// XXX Move to PyThreadState_Clear()?
18171819
clear_datastack(tstate);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp