fix(event): drain tty fd to EAGAIN before returning from try_read - #1057
Open
sinelaw wants to merge 1 commit into
Open
fix(event): drain tty fd to EAGAIN before returning from try_read#1057sinelaw wants to merge 1 commit into
sinelaw wants to merge 1 commit into
Conversation
sinelaw
force-pushed
the
fix/drain-tty-fd-to-eagain
branch
from
May 12, 2026 13:38
88cc245 to
1e4a201
Compare
sinelaw
marked this pull request as draft
May 12, 2026 13:39
mio registers the tty fd with Interest::READABLE, which on Linux maps to EPOLLIN | EPOLLET — edge-triggered. The previous read loop returned as soon as the parser produced its first event, leaving any remaining bytes in the kernel buffer. Because the edge has already been consumed, epoll_wait will not re-fire for those leftover bytes until new data arrives. In practice this surfaces as the well-known "a large paste blocks partway through and only resumes when I press a key" symptom — for example, pasting a few KB of text from tmux's paste-buffer (which does not use bracketed paste, so the bytes arrive as a flood of raw keystrokes). The first ~1 KiB is parsed, the parser returns the first event, the loop exits with bytes still in the kernel pty, and the next epoll_wait never fires. A real keypress later creates a new edge, the leftover bytes finally drain, and the paste appears to "continue". Fix: keep reading until the fd returns WouldBlock or a short read (which also indicates no more bytes are immediately available on a tty). All resulting events are queued in the parser. Only then do we return the first event to the caller; subsequent try_read calls drain the queue without any extra syscalls. No behavior change when each try_read call corresponds to a single small input (the typical interactive case): the read loop sees a short read and exits immediately, identical to the old fast path.
sinelaw
force-pushed
the
fix/drain-tty-fd-to-eagain
branch
from
May 12, 2026 13:49
1e4a201 to
623f8b4
Compare
Author
|
I would like to add a test for this but it requires some kind of harness that actually uses a real PTY. |
sinelaw
marked this pull request as ready for review
May 13, 2026 07:33
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.
mio registers the tty fd with
Interest::READABLE, which on Linux isEPOLLIN | EPOLLET(edge-triggered). The read loop returned as soon as the parser yielded its first event, leaving any remaining bytes in the kernel buffer. The edge has already been consumed, soepoll_waitwon't re-fire until new data arrives.Symptom: a large paste (e.g. tmux
paste-buffer, no bracketed paste) stalls partway through and only resumes on the next keypress, which creates a new edge and drains the leftovers.Fix: drain to
WouldBlock— or a short read, which on a tty signals the same — before returning. Events queue in the parser; subsequenttry_readcalls drain them with no extra syscalls. The typical small-input fast path is unchanged: one short read, exit.Repro inside tmux:
Tested on tmux 3.4, Linux 6.18. The
use-dev-ttypath uses level-triggeredpoll()and is unaffected.