This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python sighandlers delayed for no reason
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jcea, neologix, zdenek.pavlas
Priority: normal Keywords: patch

Created on 2012-11-26 17:12 by zdenek.pavlas, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
trip-signal.patch zdenek.pavlas, 2012-11-26 17:11
Messages (6)
msg176428 - (view) Author: Zdenek Pavlas (zdenek.pavlas) Date: 2012-11-26 17:11
Quoting from "signal" module docs:

# Although Python signal handlers are called asynchronously as far as the Python user is concerned, they can only occur between the “atomic” instructions of the Python interpreter.

Yes, that's reasonable.

# This means that signals arriving during long calculations implemented purely in C (such as regular expression matches on large bodies of text) may be delayed for an arbitrary amount of time.

IMO, A does not imply B.  Long computation in C with GIL released does not need to be atomic, as it happens "between" Python instructions.  

The thunk that's called asynchronously should preempt the C computation and issue a Python callback when possible.  Otherwise, Python handlers are useless when interfacing C code.
msg176437 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2012-11-26 17:58
Not sure if this would be a feature request or a bugfix, specially in the python 2.7 case :).
msg176438 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-11-26 18:13
> Not sure if this would be a feature request or a bugfix

That would rather be a bug injection.

This patch isn't safe: the reason why signal handlers are called synchronously from the main loop is because you can't call arbitrary called on behalf of a signal handler: the must be async safe.

The proper way to do that would be to have a thread dedicated to signal management (like the Java VM does).

This patch is invalid (as is the issue).
msg176468 - (view) Author: Zdenek Pavlas (zdenek.pavlas) Date: 2012-11-27 08:55
> This patch isn't safe

Yes, it's broken.  Does not work unless thread support was enabled, and locking initialized.  There are probably other bugs, too.  Not meant to be included, really.  But IMO the correct implementation should work along these lines.

> the reason why signal handlers are called synchronously from the main loop is because you can't call arbitrary called on behalf of a signal handler: the must be async safe.

Could you elaborate, please?  Suppose Python has called a C module.  From Python's POV, an async signal is no different from a synchronous C=>Python callback.  Both are safe.

> The proper way to do that would be to have a thread dedicated to signal management (like the Java VM does).

Please, don't.  Python is bloated enough already.

> This patch is invalid (as is the issue).

signal.alarm() and ctrl-c not working in modules is not a valid issue?
msg176470 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-11-27 09:36
> the reason why signal handlers are called synchronously from the main
loop is because you can't call arbitrary called on behalf of a signal
handler: the must be async safe.
>
>
> Could you elaborate, please?  Suppose Python has called a C module.  From
Python's POV, an async signal is no different from a synchronous C=>Python
callback.  Both are safe.

No.
Here, safe doesn't have anything to do with Python bytecode, or
thread-safety.
In C, you cannot call arbitrary code from a signal handler, the code must
be async-safe (let's say reentrant): for example, if you call malloc() from
within a signal handler, you can get a deadlock or a crash if the signal
was received while the process was in the middle of an malloc() call.
See
https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlersfor
example.

So the bottom line is that *you can't call Python code from within a signal
handler*.

> > The proper way to do that would be to have a thread dedicated to signal
management (like the Java VM does).
>
> Please, don't.  Python is bloated enough already.
>
> > This patch is invalid (as is the issue).
>
> signal.alarm() and ctrl-c not working in modules is not a valid issue?

Yes it is, but I don't think it can be solved without resorting to a
dedicated signal management thread (which would also have the nice side
effect of avoiding EINTR-related errors).
msg176474 - (view) Author: Zdenek Pavlas (zdenek.pavlas) Date: 2012-11-27 13:16
> for example, if you call malloc() from within a signal handler, you can get a deadlock or a crash if the signal was received while the process was in the middle of an malloc() call.

Thanks, I see the problem.  malloc() implements locking (when threads are enabled), but does not mask signals.  When re-entered, this deadlocks.
History
Date User Action Args
2022-04-11 14:57:38adminsetgithub: 60764
2012-11-27 13:16:25zdenek.pavlassetstatus: open -> closed

messages: + msg176474
2012-11-27 09:36:20neologixsetmessages: + msg176470
2012-11-27 08:55:09zdenek.pavlassetmessages: + msg176468
2012-11-26 18:13:20neologixsetnosy: + neologix
messages: + msg176438
2012-11-26 17:58:41jceasetnosy: + jcea

messages: + msg176437
versions: + Python 3.2, Python 3.3, Python 3.4
2012-11-26 17:12:00zdenek.pavlascreate