mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-15 17:02:03 +00:00
Fix async RAR unpacker buffer disposal
This commit is contained in:
@@ -19,11 +19,7 @@ public partial class RarArchive
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (UnpackV1.IsValueCreated && UnpackV1.Value is IDisposable unpackV1)
|
||||
{
|
||||
unpackV1.Dispose();
|
||||
}
|
||||
|
||||
DisposeUnpackers();
|
||||
_disposed = true;
|
||||
await base.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -41,20 +41,24 @@ public partial class RarArchive
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (UnpackV1.IsValueCreated && UnpackV1.Value is IDisposable unpackV1)
|
||||
{
|
||||
unpackV1.Dispose();
|
||||
}
|
||||
if (UnpackV2017.IsValueCreated && UnpackV2017.Value is IDisposable unpackV2017)
|
||||
{
|
||||
unpackV2017.Dispose();
|
||||
}
|
||||
|
||||
DisposeUnpackers();
|
||||
_disposed = true;
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeUnpackers()
|
||||
{
|
||||
if (UnpackV1.IsValueCreated && UnpackV1.Value is IDisposable unpackV1)
|
||||
{
|
||||
unpackV1.Dispose();
|
||||
}
|
||||
if (UnpackV2017.IsValueCreated && UnpackV2017.Value is IDisposable unpackV2017)
|
||||
{
|
||||
unpackV2017.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<RarArchiveEntry> LoadEntries(IEnumerable<RarVolume> volumes) =>
|
||||
RarArchiveEntryFactory.GetEntries(this, volumes, ReaderOptions);
|
||||
|
||||
|
||||
@@ -12,6 +12,17 @@ namespace SharpCompress.Readers.Rar;
|
||||
|
||||
public abstract partial class RarReader
|
||||
{
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
// The unpackers own pooled buffers even when extraction uses async I/O.
|
||||
DisposeUnpackers();
|
||||
_disposed = true;
|
||||
await base.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns file parts asynchronously for the current entry.
|
||||
/// Used for async stream operations in solid RAR archives.
|
||||
|
||||
@@ -27,20 +27,24 @@ public abstract partial class RarReader : AbstractReader<RarReaderEntry, RarVolu
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (UnpackV1.IsValueCreated && UnpackV1.Value is IDisposable unpackV1)
|
||||
{
|
||||
unpackV1.Dispose();
|
||||
}
|
||||
if (UnpackV2017.IsValueCreated && UnpackV2017.Value is IDisposable unpackV2017)
|
||||
{
|
||||
unpackV2017.Dispose();
|
||||
}
|
||||
|
||||
DisposeUnpackers();
|
||||
_disposed = true;
|
||||
base.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeUnpackers()
|
||||
{
|
||||
if (UnpackV1.IsValueCreated && UnpackV1.Value is IDisposable unpackV1)
|
||||
{
|
||||
unpackV1.Dispose();
|
||||
}
|
||||
if (UnpackV2017.IsValueCreated && UnpackV2017.Value is IDisposable unpackV2017)
|
||||
{
|
||||
unpackV2017.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void ValidateArchive(RarVolume archive);
|
||||
|
||||
public override RarVolume? Volume => volume;
|
||||
|
||||
101
tests/SharpCompress.Test/Streams/RarLifetimeTests.cs
Normal file
101
tests/SharpCompress.Test/Streams/RarLifetimeTests.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Archives.Rar;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors.Rar;
|
||||
using SharpCompress.Readers;
|
||||
using SharpCompress.Readers.Rar;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Streams;
|
||||
|
||||
public class RarLifetimeTests : TestBase
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("Rar.rar", true)]
|
||||
[InlineData("Rar.rar", false)]
|
||||
[InlineData("Rar5.rar", true)]
|
||||
[InlineData("Rar5.rar", false)]
|
||||
public async Task Reader_Async_Dispose_Releases_Unpacker_Window(string filename, bool leaveOpen)
|
||||
{
|
||||
using var input = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename));
|
||||
await using var reader = await ReaderFactory.OpenAsyncReader(
|
||||
input,
|
||||
new ReaderOptions { LeaveStreamOpen = leaveOpen },
|
||||
CancellationToken.None
|
||||
);
|
||||
while (await reader.MoveToNextEntryAsync(CancellationToken.None))
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
await reader.WriteEntryToAsync(Stream.Null, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
var property = typeof(RarReader).GetProperty(
|
||||
filename == "Rar.rar" ? "UnpackV1" : "UnpackV2017",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic
|
||||
)!;
|
||||
var unpack = ((Lazy<IRarUnpack>)property.GetValue(reader)!).Value;
|
||||
var window = GetWindow(unpack, filename);
|
||||
Assert.NotNull(window.GetValue(unpack));
|
||||
|
||||
await reader.DisposeAsync();
|
||||
await reader.DisposeAsync();
|
||||
((IDisposable)reader).Dispose();
|
||||
|
||||
Assert.Null(window.GetValue(unpack));
|
||||
Assert.Equal(leaveOpen, input.CanRead);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Rar.rar", true)]
|
||||
[InlineData("Rar.rar", false)]
|
||||
[InlineData("Rar5.rar", true)]
|
||||
[InlineData("Rar5.rar", false)]
|
||||
public async Task Archive_Async_Dispose_Releases_Unpacker_Window(
|
||||
string filename,
|
||||
bool leaveOpen
|
||||
)
|
||||
{
|
||||
using var input = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename));
|
||||
await using var archive = await RarArchive.OpenAsyncArchive(
|
||||
input,
|
||||
new ReaderOptions { LeaveStreamOpen = leaveOpen },
|
||||
CancellationToken.None
|
||||
);
|
||||
await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory))
|
||||
{
|
||||
#if LEGACY_DOTNET
|
||||
using var stream = await entry.OpenEntryStreamAsync(CancellationToken.None);
|
||||
#else
|
||||
await using var stream = await entry.OpenEntryStreamAsync(CancellationToken.None);
|
||||
#endif
|
||||
await stream.CopyToAsync(Stream.Null, Constants.BufferSize, CancellationToken.None);
|
||||
}
|
||||
|
||||
var rarArchive = (RarArchive)archive;
|
||||
var unpack = (filename == "Rar.rar" ? rarArchive.UnpackV1 : rarArchive.UnpackV2017).Value;
|
||||
var window = GetWindow(unpack, filename);
|
||||
Assert.NotNull(window.GetValue(unpack));
|
||||
|
||||
await archive.DisposeAsync();
|
||||
await archive.DisposeAsync();
|
||||
rarArchive.Dispose();
|
||||
|
||||
Assert.Null(window.GetValue(unpack));
|
||||
Assert.Equal(leaveOpen, input.CanRead);
|
||||
}
|
||||
|
||||
private static FieldInfo GetWindow(IRarUnpack unpack, string filename) =>
|
||||
unpack
|
||||
.GetType()
|
||||
.GetField(
|
||||
filename == "Rar.rar" ? "window" : "Window",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic
|
||||
)!;
|
||||
}
|
||||
Reference in New Issue
Block a user