-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-43406: Fix possible race condition where PyErr_CheckSignals
tries to execute a non-Python signal handler
#24756
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix a possible race condition where ``PyErr_CheckSignals`` tries to execute a | ||
non-Python signal handler. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1706,10 +1706,34 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate) | |
} | ||
_Py_atomic_store_relaxed(&Handlers[i].tripped, 0); | ||
|
||
/* Signal handlers can be modified while a signal is received, | ||
* and therefore the fact that trip_signal() or PyErr_SetInterrupt() | ||
* was called doesn't guarantee that there is still a Python | ||
* signal handler for it by the time PyErr_CheckSignals() is called | ||
* (see bpo-43406). | ||
*/ | ||
PyObject *func = Handlers[i].func; | ||
if (func == NULL || func == Py_None || func == IgnoreHandler || | ||
func == DefaultHandler) { | ||
/* No Python signal handler due to aforementioned race condition. | ||
* We can't call raise() as it would break the assumption | ||
* that PyErr_SetInterrupt() only *simulates* an incoming | ||
* signal (i.e. it will never kill the process). | ||
* We also don't want to interrupt user code with a cryptic | ||
* asynchronous exception, so instead just write out an | ||
* unraisable error. | ||
*/ | ||
PyErr_Format(PyExc_OSError, | ||
"Signal %i ignored due to race condition", | ||
i); | ||
PyErr_WriteUnraisable(Py_None); | ||
continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably require some consensus, but I would personally raise, as the situation is tricky enough that I think it should not pass silently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vstinner What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One problem is that re-raising will break the assumption that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's an excellent point actually. Maybe we should set an unraisable exception ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would not be much better than the asynchronous There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not set the error indicator IIRC and can be handled separately if needed by a hook. By default is like printing to stderr. The advantage would be that keeps the assumption that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, sounds good then! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to take a look at the updated patch @pablogsal . |
||
} | ||
|
||
PyObject *arglist = Py_BuildValue("(iO)", i, frame); | ||
PyObject *result; | ||
if (arglist) { | ||
result = _PyObject_Call(tstate, Handlers[i].func, arglist, NULL); | ||
result = _PyObject_Call(tstate, func, arglist, NULL); | ||
Py_DECREF(arglist); | ||
} | ||
else { | ||
|
Uh oh!
There was an error while loading. Please reload this page.