New comment by tornaria on void-packages repository https://github.com/void-linux/void-packages/pull/35730#issuecomment-1051294639 Comment: > Between the two options, I suppose I favor just disabling the SIGINT handler by default. Adding `cysignals` awareness to `prompt_toolkit` seems too special-purpose a fix; most people will not be using `cysignals` to add low-level signal handlers. My last patch is *not* adding `cysignals` awareness. On the contrary, the strategy is to save and restore the sigint handlers so that the signal state is preserved after each run of `prompt_toolkit` (instead of being reverted to the python default each time `prompt_toolkit` runs). This is a generic fix that would work for any program that sets the sigint handler, either just the python sigint handler or the whole os-level sigint handler.. > It seems to me that the breakage here is with `cysignals`, which doesn't seem to be properly overriding the existing handler when it injects its own. I don't think so. The way `cysignals` works is, at import time install a sigint handler using `sigaction(2)` as well as a python sigint handler using `signal.signal()` from python stdlib, but it expects that these handlers are not overriden -- if they are `cysignals` will not work anymore. The way `prompt_toolkit` implements the sigint handling is: each time the prompt is run, the sigint handler is added (using `loop.add_signal_handler()`) and when the prompt returns (i.e. the user hit enter to dispatch the command) the sigint handler is removed (using `loop.remove_signal_handler()`). However, "remove" means "set the handler back to python default" which disables `cysignals`. Now `prompt_toolkit` only needs its own `sigint` handler while running (i.e. while editing the prompt); and `cysignals` only needs its own `sigint` handler while running extension code so it's not a problem that `prompt_toolkit` installs a custom `sigint` handler as long as it is later restored. There should be no conflict at all, and nothing there that is specific to cysignals. The question is: how to save and restore the sigint handler? Python stdlib offers `signal.getsignal()` (to save) and `signal.signal()` (to restore), but these are NOT a complete solution as they only save and restore the python signal handlers -- the os-level signal handler will be reverted back to the standard python signal handler which is *not* good enough for `cysignals`. How to save and restore the os-level signal handler? Using `sigaction(2)`, however that needs C code but `prompt_toolkit` is python only afaict. It turns out `cysignals` has a submodule called `cysignals.pysignals` which contains functions to save and restore os-level signal handlers so this seemed like the easiest solution. This adds no dependency on `cysignals`: if not available, only the python-level signal handlers will be saved and restored. > What I would like to see from upstream are some guidance about the following questions: > > 1. Is disabling SIGINT handling by default, and having xonsh turn it on explicitly, the right course of action? I later convinced myself that this is not a good solution. In fact, ipython itself *also* crashes when receiving a `SIGINT`. Their fix also fixes ipython so it is a good one (except for not restoring the sigint handler which only affects python programs that change the sigint handler, e.g. those using `cysignals`). > 2. If not, should this be fixed in IPython by providing a configuration option that can override the default SIGINT handler? No, because ipython itself knows nothing at all about `cysignals` or `sigint` handlers, or any signal handler at all. IMHO the only proper solution is that `prompt_toolkit` restores the sigint handlers that it changes (for a good reason) to their previous values. > 3. Is this better addressed (or even addressable at all) in the signal-handling logic of `cysignals`? I don't think there's any way to fix the signal-handling logic of `cysignals`: if their sigint handler is removed then `cysignals` won't catch SIGINT at all, nothing in their logic will fix that. > If upstream doesn't respond soon, we can pick up the original patch in this PR. As I explained above, I think the second patch is better.