Skip to content

Conversation

neutrinoceros
Copy link
Contributor

@neutrinoceros neutrinoceros commented Jul 4, 2025

xref python/cpython#136248
xref neutrinoceros/inifix#421

The reproducer I used to work on this involves typer and multi-threading, and isn't consistently failing: I originally only hit the race about once in 10k tries, so this seems difficult to test systematically.
I also am not sure this rare bug warrents an entry in CHANGES.rst, but I'm glad to add it if needed.

update: scratch that, this is a similar looking but different error. My patch doesn't fix the reproducer from #824 (comment)

update 2: close #2993

@neutrinoceros neutrinoceros marked this pull request as ready for review July 4, 2025 10:24
@@ -449,7 +449,7 @@ def test_isolation_stderr_errors():
with runner.isolation() as (_, err, _):
click.echo("\udce2", err=True, nl=False)

assert err.getvalue() == b"\\udce2"
Copy link
Contributor Author

@neutrinoceros neutrinoceros Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With my patch, stream objects yielded from runner.isolation, including err, may be closed as the context manager exits, dereferencing (hence, allowing gc to collect) its internal StreamMixer reference. Comparable tests already do it this way, so I'm hoping that this change is acceptable.

@neutrinoceros neutrinoceros force-pushed the bug/testing-thread-safety branch 2 times, most recently from 345e42b to 30bb693 Compare July 4, 2025 12:51
@neutrinoceros neutrinoceros changed the base branch from main to stable July 4, 2025 12:51
@neutrinoceros neutrinoceros force-pushed the bug/testing-thread-safety branch from 30bb693 to 712f3f4 Compare July 4, 2025 12:52
@davidism davidism changed the title BUG/TST: fix a race in click.testing.StreamMixer's finalization, seen in a multi-threaded context fix a race in click.testing.StreamMixer's finalization, seen in a multi-threaded context Jul 4, 2025
@neutrinoceros neutrinoceros force-pushed the bug/testing-thread-safety branch from 712f3f4 to 4679b1c Compare July 5, 2025 07:27
@Rowlando13 Rowlando13 deleted the branch pallets:stable July 16, 2025 06:23
@Rowlando13 Rowlando13 closed this Jul 16, 2025
@Rowlando13 Rowlando13 reopened this Jul 16, 2025
@kdeldycke
Copy link
Collaborator

Looks reasonable to me to be merge as-is: as @neutrinoceros pointed out, this is hard to constantly reproduce in unit tests (which should probably be named stress tests in that case), and I doubt lots of Click users are running the code in an intensive multi-threaded context as @neutrinoceros does. So if this PR is breaking anything, we would have already caught the regression.

@neutrinoceros
Copy link
Contributor Author

I just noticed I accidentally linked #824, but again, my patch doesn't actually fix it, so it should be undone before merge.

@kdeldycke kdeldycke added the bug label Jul 21, 2025
@kdeldycke kdeldycke added this to the 8.2.2 milestone Jul 21, 2025
@Rowlando13
Copy link
Collaborator

Rowlando13 commented Jul 22, 2025

@neutrinoceros Can you unlink #824. For some reason I cannot. Once you do that I will hit merge.

@neutrinoceros
Copy link
Contributor Author

Weird. It seems I also can't.

@Rowlando13
Copy link
Collaborator

@kdeldycke can you give it a try?

@kdeldycke
Copy link
Collaborator

@kdeldycke can you give it a try?

Blocked for me to:

Screenshot 2025-07-22 at 12 01 14

@Rowlando13 Rowlando13 merged commit 737bfbd into pallets:stable Jul 22, 2025
19 checks passed
@neutrinoceros neutrinoceros deleted the bug/testing-thread-safety branch July 22, 2025 10:32
neutrinoceros added a commit to neutrinoceros/click that referenced this pull request Jul 22, 2025
Rowlando13 added a commit that referenced this pull request Jul 23, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValueError: I/O operation on closed file in testing a race condition in click.testing.StreamMixer's finalization
3 participants