Signal handlers and registration in Python

The documentation for the logging module says that

If you use asynchronous signal handlers using a signal module, you will not be able to use a record from such handlers. This is because the blocking implementations in the streaming module are not always returned and therefore cannot be called from such signal handlers.

This suggests that you do not need to make logging calls from code called by the signal handler directly or indirectly. If you do it once in a while, the program will remain in a state where only kill -9 helps.

The important question for me is now the following. Could this blocking problem also occur when other threads call logging methods while the main thread is processing the signal?

+8
python logging signals
source share
2 answers

Signal handlers require special handling when programming UNIX. Only a specific list of POSIX C functions is declared reentrant and can be called into the POSIX signal handler. IEEE Std 1003.1 lists 118 reentrant UNIX functions that you will find at https://www.opengroup.org/ (login required).

But Python signals are asynchronous: the signal module has an explanation:

Although Python signal handlers are asynchronous, since the Python user is concerned, they can only occur between the "atomic" instructions of the Python translator. This means that arrival signals during lengthy calculations are implemented exclusively in C (for example, regular expressions match on large texts) can be delayed for an arbitrary amount of time.

In this case, the signals in Python are delayed until the GIL is free.

Let's get back to your question. No , because you are using the re-entry functions in the signal processing functions. If logging is used only in threads, there will be no problems.

+7
source share

GIL (Global Interpreter Lock) prevents the execution of any Python code at the same time, so technically the main thread cannot process the signal while other threads are running. It can "appear" in this way, but there is one big mutex that allows only one python thread to run at a time.

The best thing I can guess is the problem with signals and flows is that the signal is usually caused by an interrupt that can happen at any time. So I would suggest that Python stops what it is doing and invokes the handler. At this point, a lock can already be obtained, and if logging tries to block again, you get a dead end. In some implementations, this works fine, because the mutex can be locked several times (re-entry), but others only have one lock.

Hope someone else can support this.

+2
source share

Source: https://habr.com/ru/post/650701/


All Articles