0

i have the following piece of code:

try:
    r_l, w_l, e_l = select.select([connection_fd], [], [], timeout_secs)
except select.error as (err_no, msg):
    # get interrupted on select

While my program waits on this, if there is a signal (like SIGCHLD), the select is interrupted with a select.error . However, I am not sure if this works even if I have pending signals before entering select ? (the connection_fd socket is set to blocking).

Is there a possibility that there is a pending signal but select still does not get interrupted ?

learner
  • 93
  • 8
  • 1
    what do you mean by "pending signals before entering select" ? the OS signals are low level and they are delivered to app just when arrived, no queues, no pending ones (as far as i know, correct me if i'm wrong) - so that you either receive SIGCHLD _before_ select and process it (by your own or maybe default signal handler) or receive it after entering select. no such pending signals anyway... – VB9-UANIC Feb 07 '13 at 01:17
  • I ran into this problem earlier this week. If you explicitly set a signal handler for SIGCHLD, the select.select call will be interrupted, so I added a conditional that would check to see if a SIGCHLD handler was defined and setup a no-op dummy handler if needed. – Eric Pruitt Apr 16 '16 at 22:59

1 Answers1

0

Good point. It makes me think now that signals are broken on Python :-(

VB9-UANIC: there is a notion of "pending signals" in Python, because at the C level when the signal handler is called, it just sets a flag that will be checked later at a safe point. The Python-level signal handler, if any, is only called later from one of these safe points.

This means that if we're unlucky and the signal arrives just before the C code calls the "select" system call, then the C-level signal handler is called and the flag is set, but then the C code resumes and invokes "select" --- which will not be interrupted, because there is no signal left.

If anything, it shows that signals are not reliable means of communication in Python. I don't even see how it could be fixed, btw :-(

EDIT: this is http://bugs.python.org/issue5315 .

Armin Rigo
  • 12,048
  • 37
  • 48