JsonSerializer.DeserializeAsyncEnumerable does not dispose the stream
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- backend-api-design
Research direction
Start with JsonSerializer.DeserializeAsyncEnumerableCore and its local CreateAsyncEnumerableFromArray function, then run the supplied reproduction to confirm the stream remains undisposed. Determine and document the intended configuration or default behavior, and consider the work complete when the chosen behavior is implemented and verified against the reproduction.
Written by the indexing model from the issue text.
Description
Description
The set of overloads for JsonSerializer.DeserializeAsyncEnumerable do not dispose the provided Stream, nor provide a way of configuring this. This should be classed as a bug (or at least very unexpected), as all other uses of IEnumerable and IAsyncEnumerable do dispose the inner collection or stream. The only example I could find that doesn't do this by default is XmlSerializer, although that is configurable, and StreamReader and many other classes that take a Stream offer a bool leaveOpen option as well, with false the default.
Reproduction Steps
The following code reproduces the issue, with a custom stream class that logs whether Dispose was called.
async Task Main()
{
await foreach (var i in GetStrings())
Console.WriteLine(i);
}
IAsyncEnumerable<string> GetStrings()
{
var s = new MyStream(@"[""a"",""b""]"u8.ToArray());
return JsonSerializer.DeserializeAsyncEnumerable<string>(s);
}
class MyStream : MemoryStream
{
public MyStream(byte[] array) : base(array)
{
}
protected override void Dispose(bool disposing)
{
Console.WriteLine("Disposed");
base.Dispose(disposing);
}
public override ValueTask DisposeAsync()
{
Console.WriteLine("Dispose async");
return base.DisposeAsync();
}
}
Expected behavior
"Disposed" should be logged to console
Actual behavior
It wasn't logged.
Regression?
No response
Known Workarounds
This significantly complicates passing along an IAsyncEnumerable, it means you need to write a separate iterator function to safely dispose the stream, with all the extra compiler magic to make that happen.
async IAsyncEnumerable<string> GetStrings2()
{
using var s = new MyStream(@"[""a"",""b""]"u8.ToArray());
await foreach (var str in JsonSerializer.DeserializeAsyncEnumerable<string>(s))
yield return str;
}
Configuration
.NET 9 x64
Other information
Inside JsonSerializer.DeserializeAsyncEnumerableCore there is a local function CreateAsyncEnumerableFromArray, which finishes with:
}
finally
{
bufferState.Dispose();
}
The finally just needs an extra line added
await utf8Json.DisposeAsync();
Whether this feature should be available only via an extra bool parameter, to keep the existing behaviour, is an open question. Arguably at least the default should be to dispose, because JsonSerializer eagerly reads beyond the end of the JSON, so makes little sense to keep it open any more.
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·