Skip to content

pytester: pass bytes stdin via TemporaryFile in popen - #15533

Closed
Roxen-AS wants to merge 1 commit into
pytest-dev:mainfrom
Roxen-AS:fix/pytester-stdin-pipe
Closed

Roxen-AS wants to merge 1 commit into
pytest-dev:mainfrom
Roxen-AS:fix/pytester-stdin-pipe

Conversation

@Roxen-AS

@Roxen-AS Roxen-AS commented Dec 4, 2026

Copy link
Copy Markdown

Summary

When stdin=<bytes> is passed to Pytester.popen() (or Pytester.run(), which delegates to popen()), it previously opened a pipe and called popen.stdin.write(stdin) synchronously before returning. If the child process exited without reading stdin, this raised BrokenPipeError; if stdin exceeded the OS pipe buffer (~64 KiB) and the child did not read stdin, popen.stdin.write(stdin) blocked before run() could apply timeout=.

Changes

  • In src/_pytest/pytester.py (Pytester.popen): when isinstance(stdin, bytes), write the payload into a tempfile.TemporaryFile(), seek back to 0, pass that file object as stdin to subprocess.Popen, and close the parent's handle via contextlib.ExitStack.
  • Added regression tests in testing/test_pytester.py (test_run_and_popen_large_stdin_unread) covering both pytester.run() and pytester.popen() with a 1 MiB payload when the child exits without reading stdin, times out without reading stdin, or reads the full payload.
  • Added changelog entry changelog/15472.bugfix.rst.

Verification

  • uv run pytest testing/test_pytester.py -n auto -v (56 passed, 1 skipped, 1 xfailed)
  • uv run pytest testing/test_runner.py testing/test_capture.py -n auto (199 passed, 3 skipped, 2 xfailed)
  • uvx ruff check src/_pytest/pytester.py testing/test_pytester.py (All checks passed!)
  • uvx ruff format --check src/_pytest/pytester.py testing/test_pytester.py (2 files already formatted)
  • uvx mypy --python-executable .venv\Scripts\python.exe src/_pytest/pytester.py testing/test_pytester.py (Success: no issues found in 2 source files)

Fixes #15472

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Dec 4, 2026

Copy link
Copy Markdown
Member

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Closing as a duplicate of #15501, which was linked on #15472 five days before this PR and implements the same fix with the same tests. This account opened about 30 agent-generated PRs across unrelated projects in under two weeks, without disclosure.

Our AI policy: https://github.com/pytest-dev/pytest/blob/main/CONTRIBUTING.rst#aillm-assisted-contributions-policy


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytester.run(stdin=<bytes>) raises BrokenPipeError / can block when the child doesn't read stdin

2 participants