Skip to content

Conversation

eiriktsarpalis
Copy link
Member

Fix #120010.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where enumerator disposal was not happening correctly when cancellation occurs outside of serialization, specifically addressing issue #120010. The fix ensures proper cleanup of enumerators when serialization is cancelled by external cancellation tokens.

Key changes:

  • Added cancellation token parameters to all serialization wrapper methods to support proper cancellation handling
  • Fixed disposal logic in WriteStack to prevent double disposal and handle null stack cases
  • Added comprehensive regression tests for both sync and async enumerable cancellation scenarios

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
JsonSerializerWrapper.Reflection.cs Added CancellationToken parameters to all async serialization methods and updated calls to pass tokens through
JsonSerializerWrapper.SourceGen.cs Added CancellationToken parameters to source generation wrapper methods
CollectionTests.cs Added JsonSerializable attribute for new test type
StreamingJsonSerializerWrapper.cs Updated abstract method signatures to include CancellationToken parameters
PipeJsonSerializerWrapper.cs Updated abstract method signatures to include CancellationToken parameters
CollectionTests.Generic.Write.cs Added regression test for enumerable cancellation with disposal verification
AsyncEnumerableTests.cs Added regression test for async enumerable cancellation with disposal verification
WriteStack.cs Fixed disposal logic to prevent double disposal and handle edge cases with null stack

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis eiriktsarpalis enabled auto-merge (squash) September 30, 2025 18:11
@eiriktsarpalis
Copy link
Member Author

/ba-g test failures unrelated.

@eiriktsarpalis eiriktsarpalis merged commit 201b515 into dotnet:main Oct 1, 2025
84 of 88 checks passed
@eiriktsarpalis eiriktsarpalis deleted the fix-asyncenumerable branch October 1, 2025 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonSerializer.SerializeAsync fails to dispose IAsyncEnumerator<T> on cancellation during nested serialization.

3 participants