[Python-Dev] Questions about signal handling. (original) (raw)

Eric Snow ericsnowcurrently at gmail.com
Fri Sep 21 19:01:45 EDT 2018


Hi all,

I've got a pretty good sense of how signal handling works in the runtime (i.e. via a dance with the eval loop), but still have some questions:

  1. Why do we restrict calls to signal.signal() to the main thread?
  2. Why must signal handlers run in the main thread?
  3. Why does signal handling operate via the "pending calls" machinery and not distinctly?

More details are below. My interest in the topic relates to improving in-process interpreter isolation.

#1 & #2

Toward the top of signalmodule.c we find the following comment [1] (written in 1994):

/* NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS When threads are supported, we want the following semantics:

At the very top of the file we see another relevant comment:

/* XXX Signals should be recorded per thread, now we have thread state. */

That one was written in 1997, right after PyThreadState was introduced.

So is the constraint about the main thread just a historical artifact? If not, what would be an appropriate explanation for why signals must be strictly bound to the main thread?

#3

Regarding the use of Py_MakePendingCalls() for signal handling, I can imagine the history there. However, is there any reason signal handling couldn't be separated from the "pending calls" machinery at this point? As far as I can tell there is no longer any strong relationship between the two.

-eric

[1] https://github.com/python/cpython/blob/master/Modules/signalmodule.c#L71



More information about the Python-Dev mailing list