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
Document that PyEval_RestoreThread and PyGILState_Ensure can terminate the calling thread #80608
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=Noneclosed_at=<Date2019-04-14.02:49:46.980>created_at=<Date2019-03-25.22:14:35.939>labels= ['interpreter-core', '3.7', '3.8']
title='Document that PyEval_RestoreThread and PyGILState_Ensure can terminate the calling thread'updated_at=<Date2019-04-14.02:49:46.980>user='https://github.com/pablogsal'
Currently PyEval_RestoreThread and its callers (mainly PyGILState_Ensure) can terminate the thread if the interpreter is finalizing:
PyEval_RestoreThread(PyThreadState*tstate)
{
if (tstate==NULL)
Py_FatalError("PyEval_RestoreThread: NULL tstate");
assert(gil_created());
interr=errno;
take_gil(tstate);
/* _Py_Finalizing is protected by the GIL */if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
drop_gil(tstate);
PyThread_exit_thread();
Py_UNREACHABLE();
}
errno=err;
PyThreadState_Swap(tstate);
}
This behaviour that protects against problems due to daemon threads registered with the interpreter can be *very* surprising for C-extensions that are using these functions to implement callbacks that can call into Python. These callbacks threads are not owned by the interpreter and are usually joined by someone else, ending in deadlocks in many situations.
I propose to add a warning to the documentation to inform users about this situation.
This behaviour that protects against problems due to daemon threads registered with the interpreter can be *very* surprising for C-extensions that are using these functions to implement callbacks that can call into Python.
I really dislike the design of daemon threads. If it would be only me, I would prefer this nasty feature causing so much issues at Python shutdown.
I really dislike the design of daemon threads. If it would be only me, I would prefer this nasty feature causing so much issues at Python shutdown.
Well, given that this happens as well in Python3.7 and before, at least we should document it and we can think about changing it in the future. But for now, but I suggest keeping both PRs separated (in case we really want to change anything).
Well, given that this happens as well in Python3.7 and before, at least we should document it and we can think about changing it in the future. But for now, but I suggest keeping both PRs separated (in case we really want to change anything).
Currently PyEval_RestoreThread and its callers (mainly PyGILState_Ensure)
can terminate the thread if the interpreter is finalizing:
s/interpreter/runtime/
Most likely (guaranteed?) it will be in the main interpreter, but it is actually not triggered by interpreter finalization (though it probably should be; I've opened bpo-36479).
New changeset fde9b33 by Pablo Galindo in branch 'master': bpo-36427: Document that PyEval_RestoreThread and PyGILState_Ensure can terminate the calling thread (GH-12541) fde9b33
New changeset 7723d05 by Pablo Galindo in branch '3.7':
[3.7] bpo-36427: Document that PyEval_RestoreThread and PyGILState_Ensure can terminate the calling thread (GH-12541) (GH-12820) 7723d05
Activity
pablogsal commentedon Mar 25, 2019
Currently PyEval_RestoreThread and its callers (mainly PyGILState_Ensure) can terminate the thread if the interpreter is finalizing:
This behaviour that protects against problems due to daemon threads registered with the interpreter can be *very* surprising for C-extensions that are using these functions to implement callbacks that can call into Python. These callbacks threads are not owned by the interpreter and are usually joined by someone else, ending in deadlocks in many situations.
I propose to add a warning to the documentation to inform users about this situation.
vstinner commentedon Mar 25, 2019
_Py_IsFinalizing() check is redundant :-)
vstinner commentedon Mar 25, 2019
I really dislike the design of daemon threads. If it would be only me, I would prefer this nasty feature causing so much issues at Python shutdown.
pablogsal commentedon Mar 25, 2019
Well, given that this happens as well in Python3.7 and before, at least we should document it and we can think about changing it in the future. But for now, but I suggest keeping both PRs separated (in case we really want to change anything).
vstinner commentedon Mar 25, 2019
Right. It was just a general comment :-)
pablogsal commentedon Mar 25, 2019
ericsnowcurrently commentedon Mar 29, 2019
Related:
ericsnowcurrently commentedon Mar 29, 2019
Not really:
running finalization
ericsnowcurrently commentedon Mar 29, 2019
s/interpreter/runtime/
Most likely (guaranteed?) it will be in the main interpreter, but it is actually not triggered by interpreter finalization (though it probably should be; I've opened bpo-36479).
pablogsal commentedon Apr 13, 2019
New changeset fde9b33 by Pablo Galindo in branch 'master':
bpo-36427: Document that PyEval_RestoreThread and PyGILState_Ensure can terminate the calling thread (GH-12541)
fde9b33
pablogsal commentedon Apr 14, 2019
New changeset 7723d05 by Pablo Galindo in branch '3.7':
[3.7] bpo-36427: Document that PyEval_RestoreThread and PyGILState_Ensure can terminate the calling thread (GH-12541) (GH-12820)
7723d05