-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: drain pending TTY input on shutdown to prevent DECRPM garbage #1692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisJr404
wants to merge
1
commit into
charmbracelet:main
Choose a base branch
from
ChrisJr404:fix/issue-1590-drain-input-on-shutdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //go:build darwin || dragonfly || freebsd || netbsd || openbsd | ||
| // +build darwin dragonfly freebsd netbsd openbsd | ||
|
|
||
| package tea | ||
|
|
||
| import "golang.org/x/sys/unix" | ||
|
|
||
| // drainInput discards any pending input on the TTY. It is called during | ||
| // shutdown to remove unsolicited terminal responses (e.g. DECRPM replies to | ||
| // mode 2026/2027 queries) that arrived after the input reader was cancelled. | ||
| // Without this, those bytes are read by the user's shell after exit and | ||
| // printed as garbage characters. | ||
| func (p *Program) drainInput() { | ||
| if p.ttyInput == nil { | ||
| return | ||
| } | ||
| fd := int(p.ttyInput.Fd()) | ||
| fds := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} //nolint:gosec // tty fd never overflows int32 | ||
|
|
||
| // Responses can arrive in bursts, so flush, then poll, then flush | ||
| // again until nothing more arrives within the timeout window. | ||
| // FREAD (1) tells TIOCFLUSH to discard the read queue only. | ||
| for { | ||
| _ = unix.IoctlSetPointerInt(fd, unix.TIOCFLUSH, 1) | ||
|
|
||
| n, err := unix.Poll(fds, drainTimeoutMs) | ||
| if err != nil || n <= 0 { | ||
| return | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| //go:build !windows && !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !aix | ||
| // +build !windows,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!aix | ||
|
|
||
| package tea | ||
|
|
||
| // drainInput is a no-op on platforms where we don't have a portable way to | ||
| // discard pending TTY input. | ||
| func (p *Program) drainInput() {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix | ||
| // +build darwin dragonfly freebsd linux netbsd openbsd solaris aix | ||
|
|
||
| package tea | ||
|
|
||
| // drainTimeoutMs is how long we wait for additional bytes to arrive after | ||
| // flushing the input buffer. Local terminals reply within microseconds; this | ||
| // budget exists so SSH round-trips don't slip through. | ||
| const drainTimeoutMs = 200 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //go:build linux || solaris || aix | ||
| // +build linux solaris aix | ||
|
|
||
| package tea | ||
|
|
||
| import "golang.org/x/sys/unix" | ||
|
|
||
| // drainInput discards any pending input on the TTY. It is called during | ||
| // shutdown to remove unsolicited terminal responses (e.g. DECRPM replies to | ||
| // mode 2026/2027 queries) that arrived after the input reader was cancelled. | ||
| // Without this, those bytes are read by the user's shell after exit and | ||
| // printed as garbage characters. | ||
| func (p *Program) drainInput() { | ||
| if p.ttyInput == nil { | ||
| return | ||
| } | ||
| fd := int(p.ttyInput.Fd()) | ||
| fds := []unix.PollFd{{Fd: int32(fd), Events: unix.POLLIN}} //nolint:gosec // tty fd never overflows int32 | ||
|
|
||
| // Responses can arrive in bursts, so flush, then poll, then flush | ||
| // again until nothing more arrives within the timeout window. | ||
| for { | ||
| _ = unix.IoctlSetInt(fd, unix.TCFLSH, 0) // TCIFLUSH: discard input | ||
|
|
||
| n, err := unix.Poll(fds, drainTimeoutMs) | ||
| if err != nil || n <= 0 { | ||
| return | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build windows | ||
| // +build windows | ||
|
|
||
| package tea | ||
|
|
||
| import "golang.org/x/sys/windows" | ||
|
|
||
| // drainInput discards any pending console input events to remove unsolicited | ||
| // terminal responses that arrived after the input reader was cancelled. | ||
| // Without this, those bytes can be read by the user's shell after exit and | ||
| // printed as garbage characters. | ||
| func (p *Program) drainInput() { | ||
| if p.ttyInput == nil { | ||
| return | ||
| } | ||
| _ = windows.FlushConsoleInputBuffer(windows.Handle(p.ttyInput.Fd())) | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these
// +buildlines have long been deprecated