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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);

PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);


PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 16 additions & 6 deletions Lib/test/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
# test.support.script_helper.
env = kw.setdefault('env', dict(os.environ))
env['TERM'] = 'vt100'
return subprocess.Popen(cmd_line, executable=sys.executable,
return subprocess.Popen(cmd_line,
executable=sys.executable,
text=True,
stdin=subprocess.PIPE,
stdout=stdout, stderr=stderr,
**kw)
Expand All @@ -49,12 +51,11 @@ def test_no_memory(self):
sys.exit(0)
"""
user_input = dedent(user_input)
user_input = user_input.encode()
p = spawn_repl()
with SuppressCrashReport():
p.stdin.write(user_input)
output = kill_python(p)
self.assertIn(b'After the exception.', output)
self.assertIn('After the exception.', output)
# Exit code 120: Py_FinalizeEx() failed to flush stdout and stderr.
self.assertIn(p.returncode, (1, 120))

Expand Down Expand Up @@ -86,13 +87,22 @@ def test_multiline_string_parsing(self):
</test>"""
'''
user_input = dedent(user_input)
user_input = user_input.encode()
p = spawn_repl()
with SuppressCrashReport():
p.stdin.write(user_input)
p.stdin.write(user_input)
output = kill_python(p)
self.assertEqual(p.returncode, 0)

def test_close_stdin(self):
user_input = dedent('''
import os
print("before close")
os.close(0)
''')
process = spawn_repl()
output = process.communicate(user_input)[0]
self.assertEqual(process.returncode, 0)
self.assertIn('before close', output)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix GIL usage in :c:func:`PyOS_Readline`: lock the GIL to set an exception
and pass the Python thread state when checking if there is a pending signal.
26 changes: 22 additions & 4 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,20 @@ itimer_retval(struct itimerval *iv)
#endif

static int
is_main(_PyRuntimeState *runtime)
is_main_interp(_PyRuntimeState *runtime, PyInterpreterState *interp)
{
unsigned long thread = PyThread_get_thread_ident();
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
return (thread == runtime->main_thread
&& interp == runtime->interpreters.main);
}

static int
is_main(_PyRuntimeState *runtime)
{
PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
return is_main_interp(runtime, interp);
}

static PyObject *
signal_default_int_handler(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -1726,12 +1732,14 @@ PyOS_FiniInterrupts(void)
finisignal();
}


// The caller doesn't have to hold the GIL
int
PyOS_InterruptOccurred(void)
_PyOS_InterruptOccurred(PyThreadState *tstate)
{
if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
_PyRuntimeState *runtime = &_PyRuntime;
if (!is_main(runtime)) {
if (!is_main_interp(runtime, tstate->interp)) {
return 0;
}
_Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Expand All @@ -1740,6 +1748,16 @@ PyOS_InterruptOccurred(void)
return 0;
}


// The caller must to hold the GIL
int
PyOS_InterruptOccurred(void)
{
PyThreadState *tstate = _PyThreadState_GET();
return _PyOS_InterruptOccurred(tstate);
}


static void
_clear_pending_signals(void)
{
Expand Down
Loading