Skip to content

macOS PTY stdin forwarding raises SystemError buffer overflow on key input #136

Description

@D3f0

Problem

When running a command through Context.run(..., pty=True) on macOS, keyboard input can terminate Invoke's handle_stdin worker with:

DEBUG Encountered exception SystemError('buffer overflow') in thread for 'handle_stdin'

The issue is reproducible with invoke-toolkit==0.0.68, invoke==3.0.3, and Python 3.14 on macOS. Arrow/function keys are sufficient to trigger it.

Root Cause

invoke.terminals.bytes_to_read() calls fcntl.ioctl(input_, termios.FIONREAD, b" ") when the input reports isatty() == True. On macOS, the ioctl result can exceed the two-byte buffer expected by struct.unpack("h", ...), causing Python to raise SystemError("buffer overflow").

The exception is raised inside Invoke's handle_stdin thread and then logged by the thread exception handler.

Fix

Make the terminal-byte availability probe safe on macOS/POSIX. Possible approaches:

  • use a sufficiently sized mutable buffer and unpack the documented result width;
  • avoid FIONREAD and read one byte after select() reports readiness; or
  • catch/handle the platform-specific ioctl failure and fall back to a one-byte read.

Please add a regression test that exercises bytes_to_read() or handle_stdin with a PTY and keyboard input, including escape-sequence keys.

Temporary downstream workaround: wrap the input stream so isatty() returns False, put the terminal in cbreak mode, and pass it as in_stream; this avoids the FIONREAD branch but should not be necessary for users.

Activity

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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions