Repository navigation
Description
Summary
PtySession in src/libvt/aio.py (#2, #5) writes directly to non-blocking self._master_fd via raw os.write(), which silently truncates or raises BlockingIOError on pastes exceeding kernel TTYHOG (1,024 bytes on macOS/BSD, 4,096 bytes on Linux) and can interleave synchronous feed() query replies inside multi-chunk user pastes. In addition, AsyncTerminalStream and PtySession omit expire_synchronized_output() on EOF and aclose(), inspect terminal.synchronized only at chunk end (missing committed frames when ?2026l ... ?2026h straddles one read), drop ?2048 in-band resize replies queued by PtySession.resize(), and import pty/termios eagerly along paths where lazy POSIX imports belong.
Reproduction
- Feed an unclosed
?2026hframe followed by crash output andEOFintoAsyncTerminalStream:
uv run python -c 'import asyncio; from libvt.aio import AsyncTerminalStream; r = asyncio.StreamReader(); s = AsyncTerminalStream(r); r.feed_data(b"before\x1b[?2026hCRASH_REASON"); r.feed_eof(); asyncio.run(s.pump_once()); print("sync still held:", s.terminal.synchronized, "committed:", repr(s.terminal.committed.text))'
Expected
EOFandaclose()unconditionally expire any active?2026hold sos.terminal.synchronizedbecomesFalseands.terminal.committed.textexposes'beforeCRASH_REASON'.PtySession.send(),send_event(),send_key(),paste(), and synchronous query auto-replies enqueue onto one ordered bounded FIFOdequedrained asynchronously vialoop.add_writer(master_fd, ...)with anawait session.drain()backpressure coroutine, keepingsend()andpaste()synchronous (StreamWriterstyle) while guaranteeing atomic bracketed-paste framing and zeroBlockingIOErrortruncation._frame_eventwakes whenever a committed frame counter advances (including?2026lfollowed immediately by?2026hinside a single read chunk),PtySession.resize()flushesdrain_replies()(?2048),open_pty()andaclose()closemaster_fdinside non-awaitingfinallyguards on cancellation, andimport libvt.aiosucceeds on Windows without importingpty,termios,tty, orfcntlat module load.
Actual
sync still held: True committed: 'before' — committed stays frozen at 'before' after stream EOF, and large non-blocking PtySession.paste() writes hit BlockingIOError or partial writes once kernel PTY input queues fill.
Environment
Versions
libvt:#2(0.1.0a0)- Python:
3.15.1 - OS: Linux
x86_64
Proposal
Invariants
The invariants below are load-bearing; everything under Intent is open.
PtySession.send(),send_event(),send_key(), andpaste()remain synchronous methods, paired withasync def drain(self) -> Nonefor explicit backpressure when outbound buffered bytes exceed a configurable high-water mark.- Outbound user input and auto-replies share a single FIFO queue so an auto-reply generated during
_on_readablenever splices into the middle of an atomicPasteEvent(ESC [ 200 ~ ... ESC [ 201 ~). - Reaching stream/PTY
EOFor completingaclose()always leavesterminal.synchronized == Falseand releasesmaster_fdand event-loop reader/writer registrations even ifCancelledErrorinterruptsproc.wait().
Intent (non-binding)
- Track a monotonic
committed_generation(or frame-commit counter incremented on non-synchronized feeds,?2026l, and sync expiry) onTerminalsoAsyncTerminalStreamandPtySessionwakewait_for()whenever a committed frame landed mid-chunk. - Flush
self.terminal.drain_replies()into the outbound queue at the end ofPtySession.resize().
Not doing
- Converting
PtySession.send()orpaste()intoasync defcoroutines.
Activity
aio(fix[pty]): Buffer PTY writes and expire ?2026 (#24)