Skip to content

[3.8] bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20613) #20616

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 1 commit into from
Jun 3, 2020
Merged

[3.8] bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20613) #20616

merged 1 commit into from
Jun 3, 2020

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jun 3, 2020

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)

https://bugs.python.org/issue40826

* 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)
@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.

I made the bug more likely with this local patch:

diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 119fc355ff..8e4998d459 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1737,11 +1737,12 @@ PyOS_FiniInterrupts(void)
 int
 _PyOS_InterruptOccurred(PyThreadState *tstate)
 {
+    _PyRuntimeState *runtime = &_PyRuntime;
+    if (!is_main_interp(runtime, tstate->interp)) {
+        return 0;
+    }
+
     if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
-        _PyRuntimeState *runtime = &_PyRuntime;
-        if (!is_main_interp(runtime, tstate->interp)) {
-            return 0;
-        }
         _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
         return 1;
     }

I also reproduced https://bugs.python.org/issue40826 crash using this patch (on top of this change):

diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 119fc355ff..b1d35d8c71 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1737,11 +1737,12 @@ PyOS_FiniInterrupts(void)
 int
 _PyOS_InterruptOccurred(PyThreadState *tstate)
 {
+    _PyRuntimeState *runtime = &_PyRuntime;
+    if (!is_main(runtime)) {
+        return 0;
+    }
+
     if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
-        _PyRuntimeState *runtime = &_PyRuntime;
-        if (!is_main_interp(runtime, tstate->interp)) {
-            return 0;
-        }
         _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
         return 1;
     }

With these tests, I confirm that this change fix https://bugs.python.org/issue40826

@vstinner vstinner changed the title [3.8] bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20613) [3.8] bpo-40826: Fix GIL usage in PyOS_Readline() (GH-20613) Jun 3, 2020
@vstinner
Copy link
Member Author

vstinner commented Jun 3, 2020

The PR has a NEWS entry, but the bot checking for NEWS looks sick. So I just added "skip news" label :-p

@vstinner vstinner merged commit 6f7346b into python:3.8 Jun 3, 2020
@vstinner vstinner deleted the interrupt38 branch June 3, 2020 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants