added timed drain filter to prevent garbage input after attach#39
Open
miranda wants to merge 1 commit into
Open
added timed drain filter to prevent garbage input after attach#39miranda wants to merge 1 commit into
miranda wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
When reattaching to a session running a program that uses terminal query sequences (vim, fish, etc.) the on-disk log replay writes raw escape sequences to the new terminal. atch forwards those responses to the pty as keyboard input, causing the program to interpret them as typed characters, resulting in junk input. This is easily demonstrated by detaching and reattaching a session running vim, where it will paste junk from the clipboard immediately on attach.
Fix:
After switching to raw mode and before sending MSG_ATTACH / MSG_REDRAW, run a short drain loop (default 100ms) that reads and discards anything arriving on stdin. This swallows the terminal's responses to replayed query sequences. It only watches stdin (not the socket), so it cannot interfere with pty data.
Placing the drain before MSG_ATTACH and MSG_REDRAW is critical: the SIGWINCH triggered by MSG_REDRAW causes programs like fish to redraw their prompts, which involves sending new terminal queries and expecting responses. If the drain ran during the main select loop, it would swallow those legitimate responses and cause the program to hang waiting for them.
The drain duration can be overridden with ATCH_DRAIN_MS (e.g. ATCH_DRAIN_MS=200) or disabled entirely with ATCH_DRAIN_MS=0