Skip to content

Fix ArrayPool leak with JsonDocument - #59540

Merged
steveharter merged 3 commits into
dotnet:mainfrom
steveharter:JsonDocCleanup
Sep 24, 2021
Merged

steveharter merged 3 commits into
dotnet:mainfrom
steveharter:JsonDocCleanup

Conversation

@steveharter

Copy link
Copy Markdown
Contributor

Fixes a leak of ArrayPool<byte> used by JsonDocument under certain cases.

Leak created in 6.0 and due to typo:
if (_extraRentedArrayPoolBytes != null)
should have been
if (extraRentedArrayPoolBytes != null)

This should be ported to 6.0 ASAP.

Note that ArrayPool limits the number of entries, so this is not an unbounded leak.

@ghost

ghost commented Sep 23, 2021

Copy link
Copy Markdown

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

Issue Details

Fixes a leak of ArrayPool<byte> used by JsonDocument under certain cases.

Leak created in 6.0 and due to typo:
if (_extraRentedArrayPoolBytes != null)
should have been
if (extraRentedArrayPoolBytes != null)

This should be ported to 6.0 ASAP.

Note that ArrayPool limits the number of entries, so this is not an unbounded leak.

Author: steveharter
Assignees: steveharter
Labels:

area-System.Text.Json

Milestone: 6.0.0

_utf8Json = utf8Json;
_parsedData = parsedData;

if (_extraRentedArrayPoolBytes != null)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the bug: should be extraRentedArrayPoolBytes instead of _extraRentedArrayPoolBytes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems like this is a good opportunity for a compiler warning? The compiler has knowledge that this field isn't assigned inline with the declaration, so it will always be default(T) until it's definitely assigned by a ctor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cc @dotnet/roslyn-compiler

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just because the field isn't assigned inline doesn't mean it's not assigned. Consider that the compiler has to contend with the fact that methods, including virtual ones invoked by the base type, can be used to assign fields. That is why the compiler does not generally issue "use before assign" warnings. There are just too many ways for that warning to be falsely emitted.

Sure System.Object is a nice well behaving type in .NET Core but the compiler makes no assumptions about that. It could happily decide to invoke ToString in the implicit base ctor here which could assign a bunch of fields.

More realistically though most code would invoke a property in the ctor. Even invoking a property getter can assign fields hence issuing the warning could be a false positive.

For the compiler to issue a warning it would need to assume rational implementations of System.Object and limit itself to cases when no instance methods had been invoked, no properties were invoked or more generally the this parameter wasn't used in any other function call. Pretty narrow window for the warning and overall difficult for customers to predict when it will or won't help them.

_utf8Json = ReadOnlyMemory<byte>.Empty;

if (_hasExtraRentedArrayPoolBytes)
if (_extraRentedArrayPoolBytes != null)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Keep the performance optimization; just check for null

private MetadataDb _parsedData;

private byte[]? _extraRentedArrayPoolBytes;
private bool _hasExtraRentedArrayPoolBytes;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These bools are unnecessary.

if (_extraRentedArrayPoolBytes != null)
{
byte[]? extraRentedBytes = Interlocked.Exchange(ref _extraRentedArrayPoolBytes, null);
byte[]? extraRentedBytes = Interlocked.Exchange<byte[]?>(ref _extraRentedArrayPoolBytes, null);

@steveharter steveharter Sep 23, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure why the compiler started complaining; generic inference issue caused by changing if statement check against null

@steveharter
steveharter merged commit 2e08554 into dotnet:main Sep 24, 2021
@steveharter
steveharter deleted the JsonDocCleanup branch September 24, 2021 20:06
@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants