dtsh icon indicating copy to clipboard operation
dtsh copied to clipboard

session: trap SIGINT in interactive sessions like shells do

Open pillo79 opened this issue 1 year ago • 1 comments

The current SIGINT handler in interactive sessions simply ignores it. This is not the behavior of most shells, which respond to SIGINT by restarting the prompt. This patch changes the behavior of DTSh to match that of shells.

Note: I'm unsure what the issue that is mentioned in the code with the pager is: Ctrl+C is properly trapped/handled by less in my Linux setup, and it is never passed to Python. Even trying to signal the process directly with kill -INT $(pidof python3) does not work while the pager is active. The call to self._vt.pager_exit() is thus just a precaution.

pillo79 avatar May 22 '24 14:05 pillo79

The current SIGINT handler in interactive sessions simply ignores it. This is not the behavior of most shells, which respond to SIGINT by restarting the prompt. This patch changes the behavior of DTSh to match that of shells.

Fair enough ;-)

Note: I'm unsure what the issue that is mentioned in the code with the pager is: Ctrl+C is properly trapped/handled by less in my Linux setup, and it is never passed to Python. Even trying to signal the process directly with kill -INT $(pidof python3) does not work while the pager is active. The call to self._vt.pager_exit() is thus just a precaution.

Definitely right: comments are erroneous and misleading, there is no issue with the pager (with less at least as you say), it's while reading keyboard inputs (the opposite of being in the pager, actually) that SIGINT causes issues (the KeyboardInterrupt). Your patch still fixes that, without messing with signal handlers unnecessarily, it's a better approach, besides better matching the usual behavior of shells.

Before I merge this, would you mind to also fix my poor comments, something like:

            except KeyboardInterrupt:
                # If SIGINT is signaled (e.g. CTRL-C, kill -SIGINT)
                # while reading keyboard inputs,
                # Python will in turn raise KeyboardInterrupt.
                #
                # Most shell behaviors is to just echo "^C" to the TTY
                # and start a new prompt.
                #
                # Pagers usually ingore SIGINT: the call to pager_exit()
                # is just a precaution.
                self._vt.pager_exit()
                self._vt.write("^C")
                continue

Thanks (again).

dottspina avatar May 23 '24 05:05 dottspina

Apologies it took me so long to get to this. Rebased over current dtsh-next and changed the comment.

pillo79 avatar Nov 07 '24 14:11 pillo79

@dottspina friendly ping :slightly_smiling_face:

pillo79 avatar Dec 04 '24 16:12 pillo79