WrappingStream closes the base stream even when disposeBaseStream is false

Open Beginner friendly
#6 0 comments 0 reactions 0 assignees View on GitHub

@JamBalaya56562 is already working on this.

Since Oct 24, 2026.

  • #7 by @JamBalaya56562 — open
  • #8 by @JamBalaya56562 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

Start by reading WrappingStream.Close(), Dispose(), and DisposeAsync() to trace how DisposeBaseStream affects the base stream; the issue names these entry points and includes a reproduction with TimeoutObservingStream. Check the behavior for both disposeBaseStream values across synchronous and asynchronous disposal. Done when the base stream stays open for false and is disposed correctly for true.

Written by the indexing model from the issue text.

Description

Problem

WrappingStream.Close() always calls BaseStream.Close(), without looking at DisposeBaseStream. Stream.Dispose() calls Close(), and WrappingStream.DisposeAsync() ends with base.DisposeAsync(), which also calls Close(). So:

  • with disposeBaseStream: false, the base stream is closed by Dispose(), Close(), DisposeAsync(), using and await using alike;
  • with disposeBaseStream: true, the base stream is disposed two or three times.

Close() has closed the base stream since the first version; DisposeBaseStream was added later (eb35fdf), so false has never taken effect. The current main (21240f3, v1.0.5) has the same code.

Reproduction

using Duplicati.StreamUtil;

var baseStream = new MemoryStream();
using (new TimeoutObservingStream(baseStream, disposeBaseStream: false)) { }
Console.WriteLine(baseStream.CanRead); // False, expected True

Counting the calls on the base stream (Duplicati.StreamUtil 1.0.5):

disposeBaseStream=false using       -> base: Close;Dispose(True)
disposeBaseStream=false await using -> base: Close;Dispose(True)
disposeBaseStream=false Close()     -> base: Close;Dispose(True)
disposeBaseStream=true  using       -> base: Close;Dispose(True);Close;Dispose(True)
disposeBaseStream=true  await using -> base: DisposeAsync;Close;Dispose(True);Close;Dispose(True);Close;Dispose(True)

Impact on Duplicati

Most Duplicati backends wrap the caller's stream with ObserveReadTimeout(..., false) / ObserveWriteTimeout(..., false) in a using statement, expecting the caller's stream to stay open. It is closed instead. The regular backup path is shielded (the download chain has a ShaderStream(..., keepBaseOpen: true), and an upload reopens the file on every attempt), but the remote synchronization tool and multiple destinations (LightWeightBackendManager) reuse the stream after the call. With a test backend that wraps the caller's stream the way the SSH backend does:

  • --verify-get-after-put fails (exit code 3)
  • a failed get or put is retried with the closed stream, so all retries fail ("failed after 3 attempts")
  • the rename fallback (prevent-backend-rename) fails
  • reading Position after GetAsync throws ObjectDisposedException: Cannot access a closed file

duplicati/duplicati#6806 ("Cannot access a closed file" when using multiple destinations) had the same cause; duplicati/duplicati#6809 worked around it by computing the hash before the upload.

Dominant language
C#
Stars
0
Forks
3
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.