Skip to content
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

Closed
eiriktsarpalis opened this issue Apr 7, 2021 · 5 comments · Fixed by #100194
Closed

System.Text.Json not properly disposing enumerators on exceptions #50851

eiriktsarpalis opened this issue Apr 7, 2021 · 5 comments · Fixed by #100194
Assignees
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@eiriktsarpalis
Copy link
Member

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):

[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.

@eiriktsarpalis eiriktsarpalis added this to the 6.0.0 milestone Apr 7, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Apr 7, 2021
@ghost
Copy link

ghost commented Apr 7, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

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):

[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.

Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json

Milestone: 6.0.0

@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Apr 7, 2021
@eiriktsarpalis eiriktsarpalis self-assigned this Jul 30, 2021
@eiriktsarpalis eiriktsarpalis added the enhancement Product code improvement that does NOT require public API changes/additions label Jul 30, 2021
@steveharter
Copy link
Member

Suggest moving to 7.0. No community asks that I'm aware of.

@eiriktsarpalis
Copy link
Member Author

Note that this should be fixed for Dictionary converters as well.

@eiriktsarpalis
Copy link
Member Author

Moving to Future, as we won't have time to work on this in the .NET 7 timeframe.

@layomia layomia added the help wanted [up-for-grabs] Good issue for external contributors label Dec 2, 2022
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Mar 23, 2024
@eiriktsarpalis
Copy link
Member Author

eiriktsarpalis commented Jun 24, 2024

After deliberation we've decided to not fix this bug. It is largely a theoretical concern that no user has reported so far.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale Jun 24, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants