Skip to content

"I/O operation on closed file" error with CliRunner and echo_via_pager under 8.4 #3449

Description

@mschmitzer

When echo_via_pager is used in the command, CliRunner.invoke fails with:

ValueError: I/O operation on closed file.

This started happening with Click 8.4, does not happen with 8.3.3.

Reproducer:

from click import command, echo_via_pager
from click.testing import CliRunner

@command()
def cli():
    echo_via_pager("Hello, Click!")

runner = CliRunner()
runner.invoke(cli)

Traceback:

Traceback (most recent call last):
  File ".../echo_via_pager.py", line 11, in <module>
    runner.invoke(cli)
    ~~~~~~~~~~~~~^^^^^
  File ".../.venv/echo-via-pager/lib/python3.13/site-packages/click/testing.py", line 676, in invoke
    sys.stdout.flush()
    ~~~~~~~~~~~~~~~~^^
ValueError: I/O operation on closed file.

Environment:

  • OS: Ubuntu 24.04.4, Linux 6.17
  • Python version: 3.13.5
  • Click version: 8.4

Activity

  1. frenzymadness commented on May 18, 2026

    @frenzymadness

    I ran into exactly the same issue when I tried to update click in Fedora Linux. 1 test failed, and 189 ended with errors all with the same error: ValueError: I/O operation on closed file..

  2. anlambert commented on May 18, 2026

    @anlambert
    Contributor

    Same issue on our side after upgrading click to 8.4, see test output logs.

    Looks like a regression introduced by commit c69643b as the new get_pager_file function used in echo_via_pager implementation returns a context manager wrapping a TextIO. So when the wrapped TextIO is sys.stdout, it is automatically closed when exiting the context manager.

  3. ChrisPappalardo commented on May 19, 2026

    @ChrisPappalardo
    Contributor

    Picking up this issue at pycon2026.

  4. added this to the 8.4.1 milestone on May 21, 2026
  5. added
    test runner
    on May 22, 2026
  6. kdeldycke commented on May 22, 2026

    @kdeldycke
    Collaborator

    For context, this is because #1572 was merged early without tests and the follow up #3405 did not cover this situation.

  7. kdeldycke commented on May 29, 2026

    @kdeldycke
    Collaborator

    This fix was not complete. I proposed one at #3533 . @ChrisPappalardo @anlambert @frenzymadness @mschmitzer, can you review and test it?

  8. locked as resolved and limited conversation to collaborators on Jul 24, 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

    test runner

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions