STINNER Victor added the comment:

Hum, maybe I found the root issue: the C signal handler calls 
Py_AddPendingCall() which uses a lock to protect a global static pendingcalls 
array (of 32 items). The function tries 100 times in a row to acquire to lock, 
or does nothing (returns -1) if it fails to acquire the lock.

If we start to allow signals from any thread, this shared pendingcalls array 
can quickly become a source of race conditions like deadlocks or ignored 
callbacks.

To avoid deadlocks, IMHO the best is to have a per-thread array which consumes 
512 bytes (on 64-bit, 32 items made of 2 pointers).

--

The _thread module has a strange _thread.interrupt_main() function.

--

>From the point of view of the Python signal handler, the current "if 
>(PyThread_get_thread_ident() != main_thread) return 0;" code in the C signal 
>handler is somehow an implicit pthread_sigmask(signal.SIG_BLOCK, range(1, 
>signal.NSIG)) on all threads except of the main thread, whereas Unix gives a 
>fine control on these masks with the pthread_sigmask() function.

--

The Windows part is more tricky. A Windows Event object (created by 
CreateEvent() and retrieved by _PyOS_SigintEvent()) is used to interrupt a few 
blocking functions:

* my_fgets() used by PyOS_StdioReadline() to implemented "readline" (especially 
for the REPL)
* _PyOS_WindowsConsoleReadline()
* read_console_w() of Modules/_io/winconsole.c
* time.sleep() -- only if it's the main thread and delay != 0 seconds
* _multiprocessing.SemLock.acquire() -- only if called from the main thread
* _winapi.WaitForMultipleObjects()

The event is set by the SIGINT signal handler set by Python.

Extract of pysleep() comment:

/* Allow sleep(0) to maintain win32 semantics, and as decreed
 * by Guido, only the main thread can be interrupted.
 */

----------
nosy: +pitrou

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

Reply via email to