fix: prevent Ctrl+C from killing bat when paging without -K#3447
fix: prevent Ctrl+C from killing bat when paging without -K#3447lmmx wants to merge 1 commit intosharkdp:masterfrom
-K#3447Conversation
d4ab52e to
ae1b006
Compare
BAT_PAGER doesn't include -K-K
| pub enum OutputType { | ||
| #[cfg(feature = "paging")] | ||
| Pager(Child), | ||
| Pager(PagerProc), |
There was a problem hiding this comment.
Note: this changes the public API (for bat as a library), so to be semver compatible, the next release including this cannot be a patch release
There was a problem hiding this comment.
hmm, ok. Let me know if I can alleviate any of the friction to do with that. Also: if it’s not something you’re wholly behind don’t feel obliged to merge it.
I feel like it solves the bug in terms of UX but not correct way (i.e. bat stays alive). Whether there are implications to bat staying alive I’m not quite sure though. Unlike most programs bat isn’t going to be producing a stream of data (if I follow it correctly?)
I am not fully certain how to think about the case of bat reading a large file into a pager, I wouldn’t imagine that to be a runaway process you might want to Ctrl+C to stop and then stay in the pager to read the partial result of. But I might be wrong.
If that view is right though, bat living on and just ignoring SIGINT seems “basically fine”
cff3a0c to
e4c6369
Compare
e4c6369 to
586ff04
Compare
--quit-on-intr#3444Bug
When
BAT_PAGER='less -R'is set (without-K), pressing Ctrl-C still exitslessimmediately instead of canceling the current operation and staying open. This happens because bat receives SIGINT and begins teardown, which causeslessto lose access to the TTY and exit with EIO.Solution
Use
signal-hookto temporarily ignore SIGINT in bat while the pager is active. This allowslessto handle Ctrl-C independently. When the pager exits, the signal handler is automatically restored via RAII.Specifically I registered a dummy flag that's never checked. This prevents the default SIGINT handler from terminating the process, effectively ignoring the signal.
Changes
IgnoreSigintRAII guard that ignores SIGINT while aliveChildinPagerProcstruct to carry the signal guardNow Ctrl-C behaves correctly in
less:-K: cancels search/operation, stays open-K: exits immediately (existing behavior preserved)Demo
Before: Ctrl C closes the pager but neither does the terminal fully regain control. You have to press Ctrl + C again
After: Ctrl + C sends SIGINT and bat actively ignores it, hence does not kill the pager process via teardown
Status