-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
System.Text.Json not properly disposing enumerators on exceptions #50851
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsThe current IEnumerable converters don't dispose of enumerators in certain scenaria where nested element converters throw exceptions. Reproducing test (using a few System.Text.Json.Tests components): [Fact]
public static void WriteIEnumerableT_ElementSerializationThrows_DisposesEnumerators()
{
var items = new RefCountedList<IEnumerable<int>>(Enumerable.Repeat(ThrowingEnumerable(), 1));
Assert.Throws<DivideByZeroException>(() => JsonSerializer.Serialize(items.AsEnumerable()));
Assert.Equal(0, items.RefCount); // items.RefCount evaluates to 1
static IEnumerable<int> ThrowingEnumerable()
{
yield return 42;
throw new DivideByZeroException();
}
} Note that #50778 makes changes to the serialization infrastructure to handle IEnumerator disposal in the async case, however changes on the individual converter level will still need to be made. cc @layomia @steveharter.
|
Suggest moving to 7.0. No community asks that I'm aware of. |
Note that this should be fixed for Dictionary converters as well. |
Moving to Future, as we won't have time to work on this in the .NET 7 timeframe. |
After deliberation we've decided to not fix this bug. It is largely a theoretical concern that no user has reported so far. |
Related to #48322 and #46349.
The current IEnumerable converters don't dispose of enumerators in certain scenaria where nested element converters throw exceptions. Reproducing test (using a few System.Text.Json.Tests components):
Note that #50778 makes changes to the serialization infrastructure to handle IEnumerator disposal in the async case, however changes on the individual converter level will still need to be made. cc @layomia @steveharter.
The text was updated successfully, but these errors were encountered: