Skip to content

CliRunner.invoke raises a "ValueError: I/O operation on closed file" #3110

Closed

Description

@getzze

Hi,
I am a maintainer of subliminal that uses click for its CLI. Some tests for the CLI are failing since the upgrade to click 8.3.0.
I tracked the failures to a regression in click 8.3.0 due to #2991. Reverting this commit makes my tests pass with click 8.3.0.

The failing pytest tests are of the following form:

def test_cli_download(tmp_path: os.PathLike[str]) -> None:
    runner = CliRunner()

    result = runner.invoke(subliminal_cli, ['download', '-l', 'en', '-p', 'podnapisi', 'video.mkv'])

    # Curiously this test is not failing, subtitle is even downloaded
    assert result.exit_code == 0

    # The following two asserts are failing because `result.output` show a long error message.
    assert result.output.startswith('Collecting videos')
    assert result.output.endswith('Downloaded 1 subtitle\n')

When I only run this test function with pytest, all the assertions pass, but when I run the whole file that has plenty of test functions with the same structure, all the functions fail at the result.output assert (although not consistently...)

The error message in result.output is like this:

--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.13/logging/__init__.py", line 1154, in emit
    stream.write(msg + self.terminator)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
ValueError: I/O operation on closed file.
[...]
  File "src/subliminal/score.py", line 157, in compute_score
    logger.debug('Found matches %r', matches)
Message: 'Found matches %r'
Arguments: ({'country', 'episode', 'season', 'series', 'year'},)

It seems the logging files are closed after one CliRunner.invoke call and its giving rise to failures in the other tests.

Activity

  1. edgarrmondragon commented on Oct 16, 2025

    @edgarrmondragon

    I'm seeing similar output in some projects. I think that ironically #2991 introduced a different race condition, because the stream is closed by the time logging tries to write a message to it.

    cc @neutrinoceros

  2. neutrinoceros commented on Oct 16, 2025

    @neutrinoceros
    Contributor

    Thanks for the ping. I'm not an expert here, so please take everything I say with a grain of salt, but I do notice there was a test that needed updating for click's own CI to survive my patch. Could you try using the CliRunner.isolation context manager in your tests, see if it resolves the new race ?

  3. getzze commented on Oct 16, 2025

    @getzze
    Author

    I didn't try but the docs say that the invoke method uses the isolation context, so there would be nested contexts, not sure it would help.

  4. getzze commented on Oct 16, 2025

    @getzze
    Author

    I was thinking, would it solve your problem if your patch would delete the BytesIO attributes instead of calling their close method?

  5. neutrinoceros commented on Oct 17, 2025

    @neutrinoceros
    Contributor

    I checked, and I can confirm that it wouldn't.

  6. neutrinoceros commented on Oct 17, 2025

    @neutrinoceros
    Contributor

    I didn't try but the docs say that the invoke method uses the isolation context, so there would be nested contexts, not sure it would help.

    Indeed, but to be clear, I'm suggesting to replace runner.invoke with a runner.isolation context. See for instance

    click/tests/test_testing.py

    Lines 443 to 451 in 309ce91

    def test_isolation_stderr_errors():
    """Writing to stderr should escape invalid characters instead of
    raising a UnicodeEncodeError.
    """
    runner = CliRunner()
    with runner.isolation() as (_, err, _):
    click.echo("\udce2", err=True, nl=False)
    assert err.getvalue() == b"\\udce2"

  7. getzze commented on Oct 17, 2025

    @getzze
    Author

    I've been looking at the code in testing.py, instead of overloading StreamMixer.__del__, it would be cleaner to modify BytesIOCopy to handle properly the fact that the copy_to stream can be closed or dereferenced.

    Edit: maybe allowing copy_to to be None and checking for that possibility in the methods would be enough.

  8. added a commit that references this issue on Oct 21, 2025
  9. pjonsson commented on Nov 12, 2025

    @pjonsson
    Contributor

    @neutrinoceros I tried your reproducer from #2993 with Python 3.14 (via uv) on click 8.2.1 and after a dozen runs I still haven't been able to hit the exception. Or do you mean you run that ~9 second reproducer 10 000 times, so it takes upwards of 25 hours of constant hammering to reproduce the problem once?

    Even Python's own documentation seem pretty keen on not using __del__(): https://docs.python.org/3.14/reference/datamodel.html#object.__del__

    If it takes you 9 seconds to reproduce the problem, could you please see if the following also avoids the problem you were fixing?

    diff --git a/src/click/testing.py b/src/click/testing.py
    index f6f60b8..16c021c 100644
    --- a/src/click/testing.py
    +++ b/src/click/testing.py
    @@ -77,8 +77,8 @@ class BytesIOCopy(io.BytesIO):
             self.copy_to = copy_to
     
         def flush(self) -> None:
    -        super().flush()
             self.copy_to.flush()
    +        super().flush()
     
         def write(self, b: ReadableBuffer) -> int:
             self.copy_to.write(b)
    @@ -98,7 +98,7 @@ class StreamMixer:
             self.stdout: io.BytesIO = BytesIOCopy(copy_to=self.output)
             self.stderr: io.BytesIO = BytesIOCopy(copy_to=self.output)
     
    -    def __del__(self) -> None:
    +    def close(self) -> None:
             """
             Guarantee that embedded file-like objects are closed in a
             predictable order, protecting against races between
    @@ -106,8 +106,8 @@ class StreamMixer:
     
             .. versionadded:: 8.2.2
             """
    -        self.stderr.close()
             self.stdout.close()
    +        self.stderr.close()
             self.output.close()
     
     
    @@ -429,6 +429,7 @@ class CliRunner:
                 utils.should_strip_ansi = old_should_strip_ansi  # type: ignore
                 _compat.should_strip_ansi = old__compat_should_strip_ansi
                 formatting.FORCED_WIDTH = old_forced_width
    +            stream_mixer.close()
     
         def invoke(
             self,
  10. neutrinoceros commented on Nov 12, 2025

    @neutrinoceros
    Contributor

    @neutrinoceros I tried your reproducer from #2993 with Python 3.14 (via uv) on click 8.2.1 and after a dozen runs I still haven't been able to hit the exception. Or do you mean you run that ~9 second reproducer 10 000 times, so it takes upwards of 25 hours of constant hammering to reproduce the problem once?

    No, I can reliably reproduce it under 10min. It is much easier to hit with a free-threaded interpreter. Is that what you did ?

  11. neutrinoceros commented on Nov 12, 2025

    @neutrinoceros
    Contributor

    If it takes you 9 seconds to reproduce the problem, could you please see if the following also avoids the problem you were fixing?

    I'll try this in a couple minutes, thanks !

  12. neutrinoceros commented on Nov 12, 2025

    @neutrinoceros
    Contributor

    Warmup: I could still reliably reproduce this race (got 6 hits in 70s of crunching) with Python 3.14.0t + click 8.2.0

    Pièce de résistance: I just tried your patch. Happy to report I didn't hit the race a single time with an equal amount of effort1

    Footnotes

    1. just for security fun, I also tried 10x harder, and ... 🥁 ... still no hit 🎉 ↩

  13. getzze commented on Nov 12, 2025

    @getzze
    Author

    @pjonsson thanks for the patch, but it is still giving an error with subliminal.

    Removing the call to BytesIO.close() fixes it though:

    @@ -106,8 +106,8 @@ class StreamMixer:
     
             .. versionadded:: 8.2.2
             """
    -        self.stdout.close()
    -        self.stderr.close()
    -        self.output.close()
    +        self.stdout = None
    +        self.stderr = None
    +        self.output = None

    @neutrinoceros, does it also fix the race condition in your case?

  14. neutrinoceros commented on Nov 12, 2025

    @neutrinoceros
    Contributor

    yes !

  15. pjonsson commented on Nov 12, 2025

    @pjonsson
    Contributor

    @getzze since __del__() doesn't give many guarantees, my goal was to make things deterministic so everybody runs close at the same time.

    After having looked closer at this, one problem is that the two _NamedTextIOWrapper are taking ownership of StreamMixer.stdout and StreamMixer.stderr, and the _NamedTextIOWrapper calling close() on those buffers during its destruction. I created #3139 to prevent _NamedTextIOWrapper from closing StreamMixer's buffers.

  16. pjonsson commented on Nov 12, 2025

    @pjonsson
    Contributor

    No, I can reliably reproduce it under 10min. It is much easier to hit with a free-threaded interpreter. Is that what you did ?

    I had missed the free-threaded part, if I remove your __del__ implementation I can trigger the problem in 1.7s on latest stable with the free-threaded interpreter. Thank you for taking the time to explain what the issue you had filed actually said and for testing my attempt to make things deterministic.

  17. getzze commented on Nov 12, 2025

    @getzze
    Author

    I wrote a PR that fixes to errors with subliminal tests.
    @neutrinoceros can you confirm it also fixes your bug ?
    Thanks!

  18. added this to the 8.3.2 milestone on Nov 18, 2025
  19. Priya-Yanamandra commented on Jan 4, 2026

    @Priya-Yanamandra
  20. Priya-Yanamandra commented on Jan 4, 2026

    @Priya-Yanamandra

    Hi! I tried reproducing this on the current main branch using Python 3.13 on Windows by adding tests around CliRunner.invoke with stdout/stderr writes and exceptions, but I wasn’t able to trigger the ValueError: I/O operation on closed file.

    This might be version- or environment-specific (e.g., older Click or Python versions). I’m happy to try reproducing it on a specific version if you have one in mind.

  21. added
    test runner
    on Feb 28, 2026
  22. kdeldycke commented on Feb 28, 2026

    @kdeldycke
    Collaborator

    A fix is available in #3139 and that PR is ready to be merged upstream. Can you check that your cases are fixed by that PR? And the test suite covers it?

  23. kdeldycke commented on Mar 1, 2026

    @kdeldycke
    Collaborator

    Fix has been merged in stable branch. It will be available as part of the v8.3.2 release.

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