Skip to content

Commit

Permalink
Reinstall signal handler when Python signal handler called
Browse files Browse the repository at this point in the history
  • Loading branch information
user202729 committed Feb 27, 2025
1 parent fbfc559 commit 066f8d3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cysignals/implementation.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,14 @@ static void setup_cysignals_handlers(void)
sigprocmask(SIG_SETMASK, &default_sigmask, &sigmask_with_sigint);
#endif

/* Install signal handlers */
/* Install signal handlers. We need a separate C-level interrupt handler
* apart from the Python-level interrupt handler because Python-level
* interrupt handler will not be called inside Cython code.
* See init_cysignals and python_check_interrupt for an explanation.
* A downside is the C-level interrupt handler will be overwritten
* when signal(SIGINT, getsignal(SIGINT)) is executed, in that case
* init_cysignals() need to be called again.
*/
/* Handlers for interrupt-like signals */
sa.sa_handler = cysigs_interrupt_handler;
sa.sa_flags = 0;
Expand Down
1 change: 1 addition & 0 deletions src/cysignals/signals.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cdef extern from "struct_signals.h":

ctypedef struct cysigs_t:
cy_atomic_int sig_on_count
cy_atomic_int interrupt_received
cy_atomic_int block_sigint
const char* s
PyObject* exc_value
Expand Down
6 changes: 6 additions & 0 deletions src/cysignals/signals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ def python_check_interrupt(sig, frame):
``implementation.c``.
"""
sig_check()
# If this line is reached, a possibility is that something called
# signal(SIGINT, getsignal(SIGINT)), which cause cysigs_interrupt_handler
# to not be called. We reinstall the C-level signal handler.
init_cysignals()
cysigs.interrupt_received = sig
sig_check()


cdef void verify_exc_value() noexcept:
Expand Down

0 comments on commit 066f8d3

Please sign in to comment.