Skip to content

gh-134644: handle exceptions set in PyOS_Readline #134645

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

duaneg
Copy link
Contributor

@duaneg duaneg commented May 25, 2025

The builtin input calls PyOS_Readline but seems to assume it does not set exceptions: if the call fails it checks signals and runs handlers if any are pending, which will cause an assertion failure if an exception has already been set.

Fix this by only checking signals if an exception has not already been set.

The builtin input calls `PyOS_Readline` but seems to assume it does not set
exceptions: if the call fails it checks signals and runs handlers if any are
pending, which will cause an assertion failure if an exception has already been
set.

Fix this by only checking signals if an exception has not already been set.
@duaneg
Copy link
Contributor Author

duaneg commented May 25, 2025

Since this is an internal bug fix, for a mostly a theoretical bug, that probably isn't noticeable without assertions enabled, I don't think it needs a news entry.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general idea makes sense to me. Would you mind adding a test case?

Comment on lines +2429 to 2432
if (!PyErr_Occurred())
PyErr_CheckSignals();
if (!PyErr_Occurred())
PyErr_SetNone(PyExc_KeyboardInterrupt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like if clauses that have the same condition right next to each other. Let's refactor to something like this:

Suggested change
if (!PyErr_Occurred())
PyErr_CheckSignals();
if (!PyErr_Occurred())
PyErr_SetNone(PyExc_KeyboardInterrupt);
if (!PyErr_Occurred()) {
if (PyErr_CheckSignals() == 0) {
PyErr_SetNone(PyExc_KeyboardInterrupt);
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it looks a little goofy. I'm happy to take your version, but looking at it again, what about:

if (!PyErr_Occurred() && !PyErr_CheckSignals()) {
    PyErr_SetNone(PyExc_KeyboardInterrupt);
}

@ZeroIntensity
Copy link
Member

Since this is an internal bug fix, for a mostly a theoretical bug, that probably isn't noticeable without assertions enabled, I don't think it needs a news entry.

Oops, missed this comment. We do need a blurb entry here, since this is indeed user-facing. Some people (cough cough, Gentoo users) do compile Python in release mode with assertions enabled, so there is a chance of this happening in production. I'd suggest something like "Fix assertion failure when input is interrupted by another thread."

@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 25, 2025
@duaneg
Copy link
Contributor Author

duaneg commented May 26, 2025

The general idea makes sense to me. Would you mind adding a test case?

Sure: I didn't initially because I can't figure out how to write a test that fails with the current version: interrupting the input call so it returns with an exception set is easy enough, but to actually crash it needs another signal to be received after the first has been handled, but before PyOS_Readline returns. Otherwise PyErr_Occurred() just immediately returns without running a handler, and so no assertion is triggered.

I don't really see any reliable way to achieve that with the signal handling code at present. The reproducer just spams SIGINT repeatedly to hit that window, but that will cause problems if we try it in unit tests. So, I can certainly add a test that will exercise the code, but it won't actually fail even before the fix, so I'm not sure if it is worthwhile by itself.

One thing we could consider is adding an assertion check that no exception has been set right at the top of PyErr_CheckSignals. It is not safe to call with an exception raised, but that is not checked with an assertion unless a signal is actually pending. If we check the assertion regardless of signal status it will trigger in this case, and be much more likely to catch errors like this in general.

In testing this all seems to work: the new test case crashes before the fix, works after the fix, and the rest of the test suite runs without any problems. I am hesitant about it though, given how wide an impact this would potentially have.

Oops, missed this comment. We do need a blurb entry here, since this is indeed user-facing. Some people (cough cough, Gentoo users) do compile Python in release mode with assertions enabled, so there is a chance of this happening in production. I'd suggest something like "Fix assertion failure when input is interrupted by another thread."

Fair enough, will add!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants