Skip to content

[3.9] bpo-40826: Fix GIL usage in PyOS_Readline() #20613

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

Merged
merged 2 commits into from
Jun 3, 2020
Merged

[3.9] bpo-40826: Fix GIL usage in PyOS_Readline() #20613

merged 2 commits into from
Jun 3, 2020

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jun 3, 2020

vstinner added 2 commits June 3, 2020 17:06
Fix GIL usage in PyOS_Readline(): lock the GIL to set an exception.

Pass tstate to my_fgets() and _PyOS_WindowsConsoleReadline(). Cleanup
these functions.

(cherry picked from commit c353764)
my_fgets() now calls _PyOS_InterruptOccurred(tstate) to check for
pending signals, rather calling PyOS_InterruptOccurred().

my_fgets() is called with the GIL released, whereas
PyOS_InterruptOccurred() must be called with the GIL held.

test_repl: use text=True and avoid SuppressCrashReport in
test_multiline_string_parsing().

Fix my_fgets() on Windows: fgets(fp) does crash if fileno(fp) is closed.

(cherry picked from commit fa7ab6a)
@vstinner
Copy link
Member Author

vstinner commented Jun 3, 2020

I validated manually on Linux and Windows that this change fix https://bugs.python.org/issue40826 using the interactive REPL: type import os; os.close(0), Python exits without crashing.

@vstinner vstinner merged commit 5d2396c into python:3.9 Jun 3, 2020
@vstinner vstinner deleted the interrupt39 branch June 3, 2020 15:49
vstinner added a commit that referenced this pull request Jun 3, 2020
* bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20579)

Fix GIL usage in PyOS_Readline(): lock the GIL to set an exception.

Pass tstate to my_fgets() and _PyOS_WindowsConsoleReadline(). Cleanup
these functions.

(cherry picked from commit c353764)

* bpo-40826: Add _PyOS_InterruptOccurred(tstate) function (GH-20599)

my_fgets() now calls _PyOS_InterruptOccurred(tstate) to check for
pending signals, rather calling PyOS_InterruptOccurred().

my_fgets() is called with the GIL released, whereas
PyOS_InterruptOccurred() must be called with the GIL held.

test_repl: use text=True and avoid SuppressCrashReport in
test_multiline_string_parsing().

Fix my_fgets() on Windows: fgets(fp) does crash if fileno(fp) is closed.

(cherry picked from commit fa7ab6a)
/* bpo-40826: fgets(fp) does crash if fileno(fp) is closed */
if (handle == INVALID_HANDLE_VALUE) {
return -1; /* EOF */
}
Copy link
Contributor

Choose a reason for hiding this comment

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

_get_osfhandle sets errno to EBADF if the fd is closed, so this is an error case that should return -2. That's consistent with Linux fgets for this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

my_fgets() is not a generic wrapper for fgets(), but a helper function for PyOS_StdioReadline() which doesn't use errno.

Copy link
Member Author

Choose a reason for hiding this comment

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

If you see a bug in my change, please comment https://bugs.python.org/issue40826.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just thought it would be better to be consistent with the function's API -- returning -2 for an error -- and to be consistent how the same case would behave in Linux. Also, to not include a misleading /* EOF */ comment for a case that has nothing to do with EOF.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well... the caller handles -1 and -2 the same way :-) This PR is a backport to 3.9. If you want to adjust the behavior, you can propose a PR on the master branch ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants