[PR #1152] Fix dispose methods to always set _isDisposed and call base.Dispose() when LeaveOpen is true #1592

Open
opened 2026-01-29 22:21:16 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/1152

State: closed
Merged: Yes


The dispose logic in SharpCompressStream returned early when LeaveOpen was true without setting _isDisposed or calling base.Dispose(), allowing subsequent dispose calls to re-execute disposal code.

Changes:

  • Restructured Dispose() and DisposeAsync() to always set _isDisposed and call base disposal methods
  • LeaveOpen check now only guards wrapped stream disposal and buffer cleanup
  • Verified no other dispose methods in codebase have this pattern

Before:

protected override void Dispose(bool disposing)
{
    if (_isDisposed || this.LeaveOpen)
    {
        return;  // Never sets _isDisposed or calls base.Dispose() when LeaveOpen=true
    }
    _isDisposed = true;
    base.Dispose(disposing);
    // ...
}

After:

protected override void Dispose(bool disposing)
{
    if (_isDisposed)
    {
        return;
    }
    _isDisposed = true;
    base.Dispose(disposing);
    if (this.LeaveOpen)
    {
        return;  // Skip only stream disposal and buffer cleanup
    }
    // ...
}

Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/1152 **State:** closed **Merged:** Yes --- The dispose logic in `SharpCompressStream` returned early when `LeaveOpen` was true without setting `_isDisposed` or calling `base.Dispose()`, allowing subsequent dispose calls to re-execute disposal code. **Changes:** - Restructured `Dispose()` and `DisposeAsync()` to always set `_isDisposed` and call base disposal methods - `LeaveOpen` check now only guards wrapped stream disposal and buffer cleanup - Verified no other dispose methods in codebase have this pattern **Before:** ```csharp protected override void Dispose(bool disposing) { if (_isDisposed || this.LeaveOpen) { return; // Never sets _isDisposed or calls base.Dispose() when LeaveOpen=true } _isDisposed = true; base.Dispose(disposing); // ... } ``` **After:** ```csharp protected override void Dispose(bool disposing) { if (_isDisposed) { return; } _isDisposed = true; base.Dispose(disposing); if (this.LeaveOpen) { return; // Skip only stream disposal and buffer cleanup } // ... } ``` <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/adamhathcock/sharpcompress/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.
claunia added the pull-request label 2026-01-29 22:21:16 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1592