diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs
index 1754f2f3bcfe35..fc2e4001a2a661 100644
--- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs
+++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs
@@ -59,8 +59,6 @@ public void Dispose()
if (!_leaveOpen)
{
- _archiveStream.Dispose();
-
if (_dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
@@ -68,6 +66,8 @@ public void Dispose()
s.Dispose();
}
}
+
+ _archiveStream.Dispose();
}
}
}
@@ -84,8 +84,6 @@ public async ValueTask DisposeAsync()
if (!_leaveOpen)
{
- await _archiveStream.DisposeAsync().ConfigureAwait(false);
-
if (_dataStreamsToDispose?.Count > 0)
{
foreach (Stream s in _dataStreamsToDispose)
@@ -93,6 +91,8 @@ public async ValueTask DisposeAsync()
await s.DisposeAsync().ConfigureAwait(false);
}
}
+
+ await _archiveStream.DisposeAsync().ConfigureAwait(false);
}
}
}
diff --git a/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj b/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj
index cf0cf80d2f5900..1be774fa1f487a 100644
--- a/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj
+++ b/src/libraries/System.Formats.Tar/tests/System.Formats.Tar.Tests.csproj
@@ -31,6 +31,7 @@
+
diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Async.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Async.Tests.cs
new file mode 100644
index 00000000000000..50216ed04ccbcb
--- /dev/null
+++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Async.Tests.cs
@@ -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 dataStreams = new List();
+ 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(() => ms.ReadByte());
+
+ Assert.True(dataStreams.Any());
+ foreach (Stream ds in dataStreams)
+ {
+ Assert.Throws(() => ds.ReadByte());
+ }
+ }
+
+ [Fact]
+ public async Task TarReader_LeaveOpen_True_Async()
+ {
+ await using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.pax, "many_small_files");
+ List dataStreams = new List();
+ 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();
+ }
+ }
+ }
+}
diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs
index 6de99107bd7463..479b43380939ff 100644
--- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs
+++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs
@@ -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(() => new TarReader(archiveStream: null));