-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AVRO-3802: [Csharp] Fix memory leak on deflate codec decompression #2439
Changes from 2 commits
e00d66f
d239dcc
45ddd47
42dcaf9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,17 +23,28 @@ namespace Avro.IO | |
/// <summary> | ||
/// Decoder for Avro binary format | ||
/// </summary> | ||
public partial class BinaryDecoder : Decoder | ||
public partial class BinaryDecoder : Decoder, IDisposable | ||
{ | ||
private readonly Stream stream; | ||
private readonly bool ownStream; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BinaryDecoder"/> class. | ||
/// </summary> | ||
/// <param name="stream">Stream to decode.</param> | ||
public BinaryDecoder(Stream stream) | ||
public BinaryDecoder(Stream stream) : this(stream, false) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BinaryDecoder"/> class. | ||
/// </summary> | ||
/// <param name="stream">Stream to decode.</param> | ||
/// <param name="ownStream">Leave stream open after disposing the object.</param> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me the ownStream name indicates that this object (BinaryDecoder) will now takes ownership of the stream. So ownStream means "Stream is disposed when this object is disposed". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The StreamReader constructor has a The HttpClient constructor instead has a To preserve compatibility with earlier Avro versions while being easy to understand, I think this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, it could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes a lot of sense. Will change the descriptions and Dispose logic of the encoder and decoder |
||
public BinaryDecoder(Stream stream, bool ownStream) | ||
{ | ||
this.stream = stream; | ||
this.ownStream = ownStream; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -296,5 +307,12 @@ private void Skip(long p) | |
{ | ||
stream.Seek(p, SeekOrigin.Current); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
if(!ownStream) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition IMO should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the ownStream = false in the default constructor, the stream would be disposed using the default constructor, which would make it a breaking change. |
||
stream?.Dispose(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,20 +130,22 @@ | |
} | ||
} | ||
|
||
var defaultStream = new MemoryStream(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar code like this is in GenericReader and PreresolvingDatumReader. If SpecificReader fixes the MemoryStream leak, those others should be fixed as well. |
||
var defaultEncoder = new BinaryEncoder(defaultStream); | ||
var defaultDecoder = new BinaryDecoder(defaultStream); | ||
foreach (Field rf in rs) | ||
using (var defaultStream = new MemoryStream()) | ||
{ | ||
if (writerSchema.Contains(rf.Name)) continue; | ||
var defaultEncoder = new BinaryEncoder(defaultStream); | ||
var defaultDecoder = new BinaryDecoder(defaultStream); | ||
foreach (Field rf in rs) | ||
{ | ||
if (writerSchema.Contains(rf.Name)) continue; | ||
|
||
defaultStream.Position = 0; // reset for writing | ||
Resolver.EncodeDefaultValue(defaultEncoder, rf.Schema, rf.DefaultValue); | ||
defaultStream.Flush(); | ||
defaultStream.Position = 0; // reset for reading | ||
defaultStream.Position = 0; // reset for writing | ||
Resolver.EncodeDefaultValue(defaultEncoder, rf.Schema, rf.DefaultValue); | ||
defaultStream.Flush(); | ||
defaultStream.Position = 0; // reset for reading | ||
|
||
obj = rec.Get(rf.Pos); | ||
rec.Put(rf.Pos, Read(obj, rf.Schema, rf.Schema, defaultDecoder)); | ||
obj = rec.Get(rf.Pos); | ||
Check warning Code scanning / CodeQL Dereferenced variable may be null Warning
Variable
rec Error loading related location Loading this Error loading related location Loading |
||
rec.Put(rf.Pos, Read(obj, rf.Schema, rf.Schema, defaultDecoder)); | ||
} | ||
Comment on lines
+137
to
+148
Check notice Code scanning / CodeQL Missed opportunity to use Where Note
This foreach loop
implicitly filters its target sequence Error loading related location Loading |
||
} | ||
|
||
return rec; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could make
MemoryStream inStream
read-only by constructing it withwritable: false
(the default istrue
). Not important though, as DeflateStream with CompressionMode.Decompress won't try to write to it anyway.