Skip to content

wait_for() misses a committed frame inside a single transport read #72

Backlog

Description

@tony

Summary

A synchronized-output application can commit a progress frame and replace it before the transport finishes a single read. The documented per-commit predicate can miss that first frame despite being registered before the bytes arrived.

Reproduction

  1. From the tagged libvt checkout, install the locked development dependencies with uv sync --frozen --python 3.14 --all-groups --all-extras. Use the checkout root as the working directory.

  2. Save the following as case.py at the checkout root.

import asyncio
from libvt import Terminal
from libvt.aio import AsyncTerminalStream
from libvt.exc import TerminalTimeoutError
from libvt.terminal import StopOn

def frame(text):
    return b"\x1b[?2026h\r" + text + b"\x1b[?2026l"

async def wait_for_first(data, size):
    reader = asyncio.StreamReader()
    stream = AsyncTerminalStream(reader, rows=2, cols=20, chunk_size=size)
    waiting = asyncio.create_task(stream.wait_for("draft", timeout=2, attach_note=False))
    await asyncio.sleep(0)
    reader.feed_data(data)
    reader.feed_eof()
    try:
        return (await waiting).text
    except TerminalTimeoutError as exc:
        return f"EOF={exc.eof}, final={stream.terminal.committed.text!r}"
    finally:
        await stream.aclose()

async def main():
    first = frame(b"draft")
    data = first + frame(b"saved")
    term = Terminal(rows=2, cols=20)
    offset = 0
    history = []
    while offset < len(data):
        offset = term.feed_until(data, offset=offset, stop=StopOn.SYNC_END).offset
        history.append(term.committed.text)
    print("commits:", history)
    print("one frame per read:", await wait_for_first(data, len(first)))
    print("both frames in one read:", await wait_for_first(data, len(data)))

asyncio.run(main())
  1. Run the case.
$ uv run --no-sync python case.py

Expected

A predicate promised every committed snapshot sees the same draft frame at every read size.

Actual

The parser reports two commits, draft and saved. The stream waiter sees draft with one frame per read and reaches EOF on saved when both frames arrive together.

Environment

Tested versions

CPython 3.14.6 on Linux under WSL2 ARM64. Identical output on release v0.0.1a4 (09611e5) and trunk (0864aa2).

Evidence

Observed output
commits: ['draft', 'saved']
one frame per read: draft
both frames in one read: EOF=True, final='saved'

Proposal

Step through commit boundaries before passing the remaining bytes to the parser, or expose a separately documented commit iterator and keep a settled-screen convenience. Bound the lifetime of retained snapshots for slow consumers.

  • Developer experience — A test no longer needs a custom byte splitter to observe a state the guest already committed.
  • Immediate benefit — Removes a read-size dependency from frame-sensitive tests.
  • Longer-term benefit — Aligns interactive observability with deterministic replay and high-throughput streams.
  • End-user experience — A test catches short-lived prompts and progress messages even when the operating system coalesces terminal writes.

References

Activity

  1. added
    bug
    p1
    on Nov 30, 2026
  2. tony commented on Dec 3, 2026

    @tony
    MemberAuthor

    On master, a wait matched draft when draft and saved arrived in separate reads. When one read contained both frames, the same wait reached EOF on saved. The two updates also reproduced the failure on each parent and on their combined changes.

    The frame fix builds on the shared reader in #159. On Linux it delivered draft in both read layouts. A native PTY replay delivered FRAME0449 from 450 frames and both concurrent close calls returned 0. PR #192 · PR #159

    The writer-shutdown change in #160 can land independently. The patches overlap in stream close. In the combined replay, draining frames inside the shared close task delivered the final frame and closed the writer after an input error. PR #160

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

    bugp1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions