Skip to content

Wait out prompt hooks before calling a shell busy in quit.sh - #598

Draft
ikuwow wants to merge 3 commits into
mainfrom
fix-quit-prompt-busy
Draft

ikuwow wants to merge 3 commits into
mainfrom
fix-quit-prompt-busy

Conversation

@ikuwow

@ikuwow ikuwow commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Purpose

quit.sh, run by C-q X, could stop with the session half quit and quit: pane %N runs a command in bash on the status line, although that shell sat at its prompt. It judged a shell busy from a single sample of the terminal's foreground process group, and drawing a prompt hands that group to another process for a few tens of milliseconds. A sample taken right after nvim or Claude Code exited could land in that window and end the run, in 1 of 6 runs of the reproduction under Verification.

Key changes

  • A shell now counts as idle once it holds the terminal for about 0.3 seconds without a break, and as busy when that does not happen within about 3 seconds
    • A shell running a script, or a loop of external commands typed at its prompt, hands the terminal to each command, so it never holds it that long and gets no Ctrl-D
    • A loop of builtins alone (while :; do :; done) keeps the terminal and still reads as idle, as it did with the single sample

Notes

In shells started from the .functions at this PR's base, prompts() exports PROMPT_COMMAND. A tmux shell therefore inherits the entries Ghostty's shell integration added to it (shell-integration/bash/ghostty.bash in Ghostty.app build a7e8e4d15, lines 255-276), without the functions they call. Two of those entries take the terminal at each prompt:

  • (exit "$__ghostty_status"), the fallback that runs when __ghostty_restore_status is undefined, is a subshell
    • A sample taken while the shell was away from the foreground named the holder -bash, a forked copy of the shell in a process group other than the shell's
  • __ghostty_hook is not found, so bash runs command_not_found_handle "in a separate execution environment" (bash(1), COMMAND EXECUTION)
    • In these shells declare -f command_not_found_handle shows the handler mise's activation defines, which calls mise hook-not-found

Sampling ps -o tpgid= of a pane's shell after a true builtin showed the foreground group away from the shell for these windows over 5 prompts:

round 1: busy from 28ms to 54ms
round 2: busy from 30ms to 46ms
round 3: busy from 36ms to 62ms
round 4: busy from 24ms to 32ms
round 5: busy from 28ms to 51ms

The exported PROMPT_COMMAND also doubles prompt_aws;prompt_gc, which belongs to the shell config and is left for a separate change, since quit.sh has to tolerate prompt-time processes either way.

Verification

Each reproduction run below used a private tmux server loading this repository's tmux.conf, a session laid out as ide lays it out (nvim with nvim-tree on the left, claude --ide on the right), an attached client, and quit.sh started through run-shell -b as the key binding starts it.

  • Before the change, the run failed in 1 of 6 two-pane sessions and 1 of 3 sessions with an added shell tab and Claude Code tab, each failure leaving one bash pane with quit: pane %N runs a command in bash
  • After the change, all 8 two-pane sessions and all 3 tabbed sessions closed, the tabs-store session included
  • A shell running bash -c 'sleep 30; echo SURVIVED' is still reported as quit: pane %28 runs a command in bash, and the trace shows no send-keys to it
  • idle(), called 10 times per case on a shell in a private tmux session, returned:
    • busy 10 of 10 times while it ran each of while :; do /bin/date >/dev/null; done, while :; do /bin/sleep 0.01; done, while :; do /bin/sleep 0.02; done, and for i in $(seq 100000); do /bin/sleep 0.02; done
    • idle 10 of 10 times at its prompt, and 10 of 10 times when called right after /bin/sleep 0.05 was sent, while the prompt was being drawn
  • In a real ide session started by ide in Ghostty, C-q X, then y, closed every pane: a trace of quit.sh shows nvim's :qa, Claude Code's Ctrl-D twice, and one Ctrl-D to each pane's shell within 3 seconds, and no pane was left

🤖 Generated with Claude Code

https://claude.ai/code/session_018Xu7qEsW42tJsJTDUBXwJK

ikuwow and others added 3 commits November 22, 2026 10:48
A shell drawing its prompt hands the terminal for a few tens of
milliseconds to a subshell or command-not-found handler run from
PROMPT_COMMAND. quit.sh sampled the foreground process group once, so a
sample in that window read the shell as running a command and ended the
run with the session half quit. idle now polls for up to 2 seconds and
reports busy only when the shell keeps the terminal away the whole time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Xu7qEsW42tJsJTDUBXwJK
A shell running a loop typed at its prompt takes the terminal back for
an instant between the loop's commands, so accepting any one idle
sample in 2 seconds read such a shell as idle and left a Ctrl-D in its
input. A prompt keeps the terminal once its hooks finish, so 3 samples
in a row, 0.1 seconds apart, tell the two apart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Xu7qEsW42tJsJTDUBXwJK
A loop of fast external commands typed at the prompt gives the terminal
back to the shell often enough that 3 samples 0.1 seconds apart could
all land on it. Sampling every 0.01 seconds and asking for 20 in a row
needs the shell to hold the terminal on end, which a loop handing it to
each command never does. The comment also names the loop of builtins
that still reads as idle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Xu7qEsW42tJsJTDUBXwJK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant