Github messages for voidlinux
 help / color / mirror / Atom feed
From: tornaria <tornaria@users.noreply.github.com>
To: ml@inbox.vuxu.org
Subject: Re: python3-prompt_toolkit: don't handle sigint
Date: Fri, 25 Feb 2022 22:46:02 +0100	[thread overview]
Message-ID: <20220225214602.kT3Zt93KR9TIlmjs0rvIRcOgrrIiKM54uBqIkYhQpaU@z> (raw)
In-Reply-To: <gh-mailinglist-notifications-41a7ca26-5023-4802-975b-f1789d68868e-void-packages-35730@inbox.vuxu.org>

[-- Attachment #1: Type: text/plain, Size: 4316 bytes --]

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.





  parent reply	other threads:[~2022-02-25 21:46 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21 14:03 [PR PATCH] " tornaria
2022-02-21 14:11 ` ahesford
2022-02-21 14:11 ` ahesford
2022-02-21 14:35 ` tornaria
2022-02-24 20:59 ` [PR PATCH] [Updated] " tornaria
2022-02-24 21:01 ` tornaria
2022-02-25 15:53 ` ahesford
2022-02-25 21:46 ` tornaria [this message]
2022-02-25 22:11 ` ahesford
2022-02-27 18:19 ` tornaria
2022-02-27 21:48 ` ahesford
2022-02-27 21:49 ` ahesford
2022-02-27 21:49 ` ahesford
2022-02-28  1:35 ` [PR PATCH] [Updated] " tornaria
2022-02-28  1:59 ` tornaria
2022-02-28  2:25 ` [PR PATCH] [Merged]: " ahesford
2022-02-28  2:28 ` ahesford

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220225214602.kT3Zt93KR9TIlmjs0rvIRcOgrrIiKM54uBqIkYhQpaU@z \
    --to=tornaria@users.noreply.github.com \
    --cc=ml@inbox.vuxu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).