Skip to content

Dispose archive stream after the list of DataStreams #80572

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

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public void Dispose()

if (!_leaveOpen)
{
_archiveStream.Dispose();

if (_dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
{
s.Dispose();
}
}

_archiveStream.Dispose();
}
}
}
Expand All @@ -84,15 +84,15 @@ public async ValueTask DisposeAsync()

if (!_leaveOpen)
{
await _archiveStream.DisposeAsync().ConfigureAwait(false);

if (_dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
{
await s.DisposeAsync().ConfigureAwait(false);
}
}

await _archiveStream.DisposeAsync().ConfigureAwait(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="TarFile\TarFile.CreateFromDirectory.Stream.Tests.cs" />
<Compile Include="TarFile\TarFile.ExtractToDirectory.File.Tests.cs" />
<Compile Include="TarFile\TarFile.ExtractToDirectory.Stream.Tests.cs" />
<Compile Include="TarReader\TarReader.Async.Tests.cs" />
<Compile Include="TarReader\TarReader.StreamConformanceTests.cs" />
<Compile Include="TarReader\TarReader.File.GlobalExtendedAttributes.Async.Tests.cs" />
<Compile Include="TarReader\TarReader.File.Async.Tests.Base.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace System.Formats.Tar.Tests
{
public partial class TarReader_Tests : TarTestsBase
{
[Fact]
public async Task TarReader_LeaveOpen_False_Async()
{
await using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files");
List<Stream> dataStreams = new List<Stream>();
await using (TarReader reader = new TarReader(ms, leaveOpen: false))
{
TarEntry entry;
while ((entry = await reader.GetNextEntryAsync()) != null)
{
if (entry.DataStream != null)
{
dataStreams.Add(entry.DataStream);
}
}
}

Assert.Throws<ObjectDisposedException>(() => ms.ReadByte());

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
Assert.Throws<ObjectDisposedException>(() => ds.ReadByte());
}
}

[Fact]
public async Task TarReader_LeaveOpen_True_Async()
{
await using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files");
List<Stream> dataStreams = new List<Stream>();
await using (TarReader reader = new TarReader(ms, leaveOpen: true))
{
TarEntry entry;
while ((entry = await reader.GetNextEntryAsync()) != null)
{
if (entry.DataStream != null)
{
dataStreams.Add(entry.DataStream);
}
}
}

ms.ReadByte(); // Should not throw

Assert.True(dataStreams.Any());
foreach (Stream ds in dataStreams)
{
ds.ReadByte(); // Should not throw
ds.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.Formats.Tar.Tests
{
public class TarReader_Tests : TarTestsBase
public partial class TarReader_Tests : TarTestsBase
{
[Fact]
public void TarReader_NullArchiveStream() => Assert.Throws<ArgumentNullException>(() => new TarReader(archiveStream: null));
Expand Down