Skip to content

Commit 8c08996

Browse files
committed
bash: Mark shell sessions as active on command start
Previously shell sessions were considered inactive until the first interactive command has exited. This caused false positives when long running first command was killed because shell session was considered inactive. Now shell session is considered active when the first interactive command is started. The code is somewhat ugly because it includes workaround for known bugs.
1 parent ad63e75 commit 8c08996

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

dotfiles/bash/bashrc.copy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# BASH CONFIGURATION FILE
22

3+
# Source this file only once (most useful when linked to from ~/.profile and ~/.bash_profile)
4+
if [[ -n "$COMMON_LOADED" ]]; then return; fi
5+
COMMON_LOADED=yes
6+
37
# Load configuration from Git repo
48
COMMON="$HOME/.common"
59
. "$COMMON/dotfiles/bash/bashrc"

dotfiles/bash/interactive

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,27 @@ shopt -s globstar
3131
# Exit interactive sessions that were never used (forgotten tmux panes,
3232
# VSCode terminals, etc).
3333
# If shell session was used for at least one command TMOUT will be disabled.
34-
TMOUT_EXIT_FORGOTTEN_SHELL="${TMOUT_EXIT_FORGOTTEN_SHELL:-900}" # 15 minutes
35-
TMOUT="$TMOUT_EXIT_FORGOTTEN_SHELL"
36-
_exit_forgotten_shell() {
37-
if [[ -n "$_INTERACTIVE_COMMANDS_ENTERED" ]]
38-
then
39-
return
40-
fi
41-
_INTERACTIVE_COMMANDS_ENTERED=some
42-
TMOUT=0
43-
}
44-
PROMPT_COMMAND="_exit_forgotten_shell; $PROMPT_COMMAND"
34+
TMOUT_FORGOTTEN_SHELL="${TMOUT_FORGOTTEN_SHELL:-900}" # 15 minutes
35+
if [[ "$TMOUT_FORGOTTEN_SHELL" -gt 0 ]]
36+
then
37+
TMOUT="$TMOUT_FORGOTTEN_SHELL"
38+
unset TMOUT_FORGOTTEN_SHELL
39+
40+
PROMPT_COMMAND_FORGOTTEN_SHELL="$PROMPT_COMMAND"
41+
_reset_forgotten_shell_detection() {
42+
if [[ -v PROMPT_COMMAND_FORGOTTEN_SHELL ]]
43+
then
44+
PROMPT_COMMAND="$PROMPT_COMMAND_FORGOTTEN_SHELL"
45+
unset PROMPT_COMMAND_FORGOTTEN_SHELL
46+
fi
47+
unset -f _reset_forgotten_shell_detection
48+
unset -f _this_shell_is_not_forgotten
49+
}
50+
51+
_this_shell_is_not_forgotten() {
52+
TMOUT=0
53+
PROMPT_COMMAND="trap - DEBUG; _reset_forgotten_shell_detection; $PROMPT_COMMAND_FORGOTTEN_SHELL"
54+
trap - DEBUG # trap does not really untrap from trap, hence the delegation to PROMPT_COMMAND above
55+
}
56+
PROMPT_COMMAND="trap _this_shell_is_not_forgotten DEBUG; $PROMPT_COMMAND"
57+
fi

0 commit comments

Comments
 (0)