Repository navigation
Fix ArrayPool leak with JsonDocument - #59540
Conversation
|
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsFixes a leak of Leak created in 6.0 and due to typo: This should be ported to 6.0 ASAP. Note that ArrayPool limits the number of entries, so this is not an unbounded leak.
|
| _utf8Json = utf8Json; | ||
| _parsedData = parsedData; | ||
|
|
||
| if (_extraRentedArrayPoolBytes != null) |
There was a problem hiding this comment.
This is the bug: should be extraRentedArrayPoolBytes instead of _extraRentedArrayPoolBytes
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
cc @dotnet/roslyn-compiler
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Keep the performance optimization; just check for null
| private MetadataDb _parsedData; | ||
|
|
||
| private byte[]? _extraRentedArrayPoolBytes; | ||
| private bool _hasExtraRentedArrayPoolBytes; |
There was a problem hiding this comment.
These bools are unnecessary.
| if (_extraRentedArrayPoolBytes != null) | ||
| { | ||
| byte[]? extraRentedBytes = Interlocked.Exchange(ref _extraRentedArrayPoolBytes, null); | ||
| byte[]? extraRentedBytes = Interlocked.Exchange<byte[]?>(ref _extraRentedArrayPoolBytes, null); |
There was a problem hiding this comment.
Not sure why the compiler started complaining; generic inference issue caused by changing if statement check against null
Fixes a leak of
ArrayPool<byte>used byJsonDocumentunder 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.