STINNER Victor <vstin...@python.org> added the comment:

So PyOS_InterruptOccurred() must be called with the GIL held since 3.8, it's 
just that the nobody noticed the bug before.

If SIGGINT is tripped and the GIL is released, PyOS_InterruptOccurred() does 
also crash in Python 3.8.

--

One way to see the bug in Python 3.8 without having to trip SIGINT.

Apply this patch:

diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 0c9a2671fe..b850af3163 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1729,11 +1729,12 @@ PyOS_FiniInterrupts(void)
 int
 PyOS_InterruptOccurred(void)
 {
+    _PyRuntimeState *runtime = &_PyRuntime;
+    if (!is_main(runtime)) {
+        return 0;
+    }
+
     if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
-        _PyRuntimeState *runtime = &_PyRuntime;
-        if (!is_main(runtime)) {
-            return 0;
-        }
         _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
         return 1;
     }

$ make
$ find -name "*readline*so" -delete
$ ./python
Python 3.8.3+ (heads/3.8-dirty:00a240bf7f, Jun  1 2020, 17:24:09) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os; os.close(0)
>>>
Erreur de segmentation (core dumped)

---

Reproduce the bug in Python 3.8 without modifying the code, using gdb to 
trigger events SIGINT at the right place:

$ gdb ./python
(gdb) b my_fgets
Breakpoint 1 at 0x68b941: file Parser/myreadline.c, line 39.

(gdb) run
Python 3.8.3+ (heads/3.8:00a240bf7f, Jun  1 2020, 17:27:24) 
[GCC 10.1.1 20200507 (Red Hat 10.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Breakpoint 1, my_fgets (...)
(gdb) p (void)close(0)

(gdb) delete 1

(gdb) signal SIGINT
Continuing with signal SIGINT.

Program received signal SIGSEGV, Segmentation fault.
is_main (runtime=0x7ff6c0 <_PyRuntime>) at ./Modules/signalmodule.c:193
193         PyInterpreterState *interp = 
_PyRuntimeState_GetThreadState(runtime)->interp;

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40826>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to