|
|
(#4818)
* fix: handle short writes in write_to_tty_stdin to prevent paste truncation
On macOS the kernel PTY buffer is only 4-16 KiB, so a single write()
call for a large paste returns a short write count rather than blocking
until all bytes are accepted. The current code returns this partial
count without retrying, silently dropping the remaining bytes.
This wraps the write in a retry loop that handles:
- Short writes (advances the buffer offset and retries)
- EINTR (retries immediately)
- EAGAIN (sleeps 1ms then retries)
Without this fix, pasting ~1 KiB+ of text into a terminal pane loses
everything beyond the first kernel-buffer-full.
Includes a test that exercises the fix using a real PTY pair with
O_NONBLOCK set on the master fd to force short writes.
* fix: cap EAGAIN retries at 50 to prevent infinite busy-wait
Per reviewer feedback, add a retry counter that caps consecutive EAGAIN
retries at 50 and logs an error if the limit is reached. The counter
resets on any successful write progress, so it only triggers when the
PTY is genuinely stuck.
* fix: use poll(POLLOUT) instead of sleep-retry loop for EAGAIN handling
Replace the 50-retry sleep(1ms) busy-wait with poll(POLLOUT, 5s timeout).
This properly waits for fd writability without CPU spinning and returns
an error on timeout instead of silently returning Ok(partial) — which
the caller (pty_writer_main) would ignore, causing silent data loss.
Tests:
- write_to_tty_stdin_delivers_all_bytes: remove flaky reader delay,
use concurrent reader, move master close after reader join to avoid
macOS kernel buffer flush race
- write_all_to_fd_errors_on_stuck_pty: new test verifying timeout
returns Err on stuck PTY (uses 100ms timeout for speed)
* fix: re-queue partial PTY writes instead of blocking PtyWriter thread
Replace the poll(POLLOUT) blocking loop with a non-blocking re-queue
approach as suggested in review. On partial write or EAGAIN:
- write_to_tty_stdin now drains as much as the kernel accepts in one
pass and returns the count (may be 0 on EAGAIN)
- PtyWriter re-queues unwritten bytes back onto its own channel,
letting other terminals' writes proceed between retries
- Retry count tracks consecutive zero-progress attempts (reset on
any forward progress), drops bytes after 50 retries with log::error
- yield_now() before re-queue on zero progress to avoid tight spin
- Guard against write() returning Ok(0) on non-empty buffer
- Wire up PtyWriter's self-sender (was None, now Some(&to_pty_writer))
* fix: per-terminal write buffering to handle short writes and EAGAIN
Replace re-queue approach with per-terminal pending write buffers
(HashMap<u32, VecDeque<PendingWrite>>). Uses recv_timeout(10ms) when
pending data exists, blocking recv when idle. 10MB per-terminal cap
drops buffer on overflow. Terminal errors clear the queue.
Adds Bus::recv_timeout via crossbeam Select::select_timeout.
Removes tcdrain from write path.
* use safe accessor methods and saturating substitutions
* add tests for ptywriter
* rustfmt
* fix tests by not assuming kernel buffer sizes
* rustfmt
* add pr
---------
Co-authored-by: Aram Drevekenin <aram@poor.dev>
|