Skip to content

Commit afc286d

Browse files
authored
fix(examples): discard a SIGALRM still pending as _deadline puts the handler back (#2090) (#2091)
_deadline.__exit__ cancelled the alarm and restored the previous handler directly, so on GraalPy a SIGALRM that fired just before the cancel could be delivered to SIG_DFL afterwards and end the worker, as in #2084. Ignoring the signal first discards it. The new test holds it pending with pthread_sigmask.
1 parent e2786bc commit afc286d

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎examples/generators/options.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,11 @@ def __exit__(self, *exc_info: 'Any') -> 'None':
19271927
# Cancel before restoring, so an alarm firing between the two
19281928
# cannot be delivered to whatever handler was installed before.
19291929
signal.alarm(0)
1930+
# Cancelling does not withdraw an alarm that has fired but is not yet
1931+
# delivered, which on GraalPy it can still be: delivered once the
1932+
# previous handler is back -- ``SIG_DFL``, under pytest -- it ends the
1933+
# worker (#2084, #2090). Ignoring the signal discards a pending one.
1934+
signal.signal(signal.SIGALRM, signal.SIG_IGN)
19301935
signal.signal(signal.SIGALRM, self.previous)
19311936
self.armed = False
19321937

‎tests/protocols/test_option_generator_deadline_scale_1428_unit.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,41 @@ def firing_alarm(seconds: int) -> int:
528528
{'raised': result, 'pending': pending, 'restored': handler is self.handler},
529529
{'raised': None, 'pending': 0, 'restored': True})
530530

531+
@unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), 'needs signal.pthread_sigmask')
532+
def test_a_firing_still_pending_as_the_deadline_cancels_is_discarded(self) -> None:
533+
"""#2090: a firing not yet delivered by the restore is never delivered.
534+
535+
GraalPy delivers a signal asynchronously, so one that fired just before the
536+
cancel can still be pending once :meth:`_deadline.__exit__` has put the
537+
previous handler back -- ``SIG_DFL``, under pytest, which ends the worker
538+
(#2084). Blocking the signal stands in for that delay: it is sent from
539+
inside the cancel and stays pending until the ``with`` has been left, and
540+
the handler put back must not then receive it.
541+
"""
542+
real_alarm = signal.alarm
543+
handled = [] # type: list[int]
544+
signal.signal(signal.SIGALRM, lambda signum, frame: handled.append(signum))
545+
sent = [] # type: list[int]
546+
547+
def firing_alarm(seconds: int) -> int:
548+
if not seconds and not sent:
549+
sent.append(signal.SIGALRM)
550+
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGALRM})
551+
signal.pthread_kill(threading.get_ident(), signal.SIGALRM)
552+
return real_alarm(seconds)
553+
554+
try:
555+
with mock.patch.object(signal, 'alarm', firing_alarm):
556+
with self.options._deadline(5): # pylint: disable=protected-access
557+
pass
558+
finally:
559+
# Unblocked, a signal still pending is delivered before this returns.
560+
signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGALRM})
561+
pending = real_alarm(0)
562+
signal.signal(signal.SIGALRM, self.handler)
563+
self.assertEqual({'sent': sent, 'handled': handled, 'pending': pending},
564+
{'sent': [signal.SIGALRM], 'handled': [], 'pending': 0})
565+
531566

532567
if __name__ == '__main__':
533568
unittest.main()

0 commit comments

Comments
 (0)