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

gh-134644: handle exceptions set inPyOS_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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
duaneg wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromduaneg:gh-134644

Conversation

duaneg
Copy link
Contributor

@duanegduaneg commentedMay 25, 2025
edited by bedevere-appbot
Loading

The builtin input callsPyOS_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 setexceptions: if the call fails it checks signals and runs handlers if any arepending, which will cause an assertion failure if an exception has already beenset.Fix this by only checking signals if an exception has not already been set.
@duaneg
Copy link
ContributorAuthor

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

@ZeroIntensityZeroIntensity 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);

Choose a reason for hiding this comment

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

I don't likeif 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
ContributorAuthor

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 wheninput is interrupted by another thread."

@ZeroIntensityZeroIntensity added needs backport to 3.13bugs and security fixes needs backport to 3.14bugs and security fixes labelsMay 25, 2025
@duaneg
Copy link
ContributorAuthor

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 theinput 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 beforePyOS_Readline returns. OtherwisePyErr_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 spamsSIGINT 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 ofPyErr_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 wheninput is interrupted by another thread."

Fair enough, will add!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@ericsnowcurrentlyericsnowcurrentlyAwaiting requested review from ericsnowcurrentlyericsnowcurrently is a code owner

Assignees
No one assigned
Labels
awaiting reviewneeds backport to 3.13bugs and security fixesneeds backport to 3.14bugs and security fixes
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@duaneg@ZeroIntensity

[8]ページ先頭

©2009-2025 Movatter.jp