Skip to content

Buffer PtySession writes via add_writer and expire ?2026 holds on EOF #24

Done

Description

@tony

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

  1. Feed an unclosed ?2026h frame followed by crash output and EOF into AsyncTerminalStream:
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

  • EOF and aclose() unconditionally expire any active ?2026 hold so s.terminal.synchronized becomes False and s.terminal.committed.text exposes 'beforeCRASH_REASON'.
  • PtySession.send(), send_event(), send_key(), paste(), and synchronous query auto-replies enqueue onto one ordered bounded FIFO deque drained asynchronously via loop.add_writer(master_fd, ...) with an await session.drain() backpressure coroutine, keeping send() and paste() synchronous (StreamWriter style) while guaranteeing atomic bracketed-paste framing and zero BlockingIOError truncation.
  • _frame_event wakes whenever a committed frame counter advances (including ?2026l followed immediately by ?2026h inside a single read chunk), PtySession.resize() flushes drain_replies() (?2048), open_pty() and aclose() close master_fd inside non-awaiting finally guards on cancellation, and import libvt.aio succeeds on Windows without importing pty, termios, tty, or fcntl at 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(), and paste() remain synchronous methods, paired with async def drain(self) -> None for 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_readable never splices into the middle of an atomic PasteEvent (ESC [ 200 ~ ... ESC [ 201 ~).
  • Reaching stream/PTY EOF or completing aclose() always leaves terminal.synchronized == False and releases master_fd and event-loop reader/writer registrations even if CancelledError interrupts proc.wait().

Intent (non-binding)

  • Track a monotonic committed_generation (or frame-commit counter incremented on non-synchronized feeds, ?2026l, and sync expiry) on Terminal so AsyncTerminalStream and PtySession wake wait_for() whenever a committed frame landed mid-chunk.
  • Flush self.terminal.drain_replies() into the outbound queue at the end of PtySession.resize().

Not doing

  • Converting PtySession.send() or paste() into async def coroutines.

References

Activity

  1. added
    bug
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions