Skip to content

Implement Rx-style auto-detach behavior for observables - #98

Merged
Tyrrrz merged 15 commits into
primefrom
copilot/implement-auto-detach-behavior
Jul 29, 2026
Merged

Implement Rx-style auto-detach behavior for observables#98
Tyrrrz merged 15 commits into
primefrom
copilot/implement-auto-detach-behavior

Conversation

Copilot AI commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Producers that rely on their returned IDisposable to stop work (e.g. CliWrap cancelling an underlying command) were leaking subscriptions when a consumer callback threw — the upstream subscription was never disposed.

Changes

PowerKit/Observable.cs

  • AutoDetachObserver<T> (new, file-scoped): wraps an IObserver<T> and owns a single-assignment source disposable.
    • OnNext — disposes source in catch on throw, rethrows.
    • OnError / OnCompleted — disposes source in finally (success and throw).
    • SetDisposable — atomically assigns the source disposable; if disposal was already flagged (synchronous emission threw before subscribe returned), disposes immediately.
  • Observable.Create<T> — now wraps the supplied observer in AutoDetachObserver<T> and assigns the returned disposable via SetDisposable.
  • Observable.CreateSynchronized<T> — unchanged; delegates to Create, inheriting auto-detach. Resulting chain: SynchronizedObserver → AutoDetachObserver → original observer.

PowerKit.Tests/ObservableTests.cs

Eight new tests covering:

  • OnNext / OnError / OnCompleted throwing — source disposed and exception rethrown.
  • Successful OnError / OnCompleted — source disposed.
  • Synchronous OnNext during subscribe throws before disposable is returned — disposable disposed immediately once assigned.
  • CreateSynchronized — same auto-detach semantics verified.

Example

var observable = Observable.Create<int>(observer =>
{
    // ... start async work ...
    return Disposable.Create(() => CancelWork());  // must run on failure
});

// Previously: if OnNext threw, CancelWork() was never called.
// Now: auto-detach disposes the source subscription, calling CancelWork().
observable.Subscribe(new FakeObserver<int>(
    onNext: _ => throw new InvalidOperationException()
));

Copilot AI changed the title feat: Rx-style auto-detach behavior for PowerKit observables feat: Rx-style auto-detach behavior for observables Jul 27, 2026
Copilot AI requested a review from Tyrrrz July 27, 2026 17:53
@Tyrrrz Tyrrrz added the bug Something isn't working label Jul 27, 2026
Comment thread PowerKit/Observable.cs Outdated
Comment thread PowerKit/Observable.cs Outdated
@Tyrrrz Tyrrrz changed the title feat: Rx-style auto-detach behavior for observables Implement Rx-style auto-detach behavior for observables Jul 28, 2026
Comment thread PowerKit/AutoDetachObserver.cs

@Tyrrrz Tyrrrz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread PowerKit/AutoDetachObserver.cs Outdated
Comment thread PowerKit/AutoDetachObserver.cs Outdated
Comment thread PowerKit/AutoDetachObserver.cs Outdated
Comment thread PowerKit/AutoDetachObserver.cs Outdated
Comment thread PowerKit.Tests/ObservableTests.cs Outdated
subscription.Dispose();
var observable = Observable.Create<int>(observer =>
{
new Thread(() =>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not Task.Run instead? @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to Task.Run in the latest commit. No reason to spin up a dedicated thread here — threadpool is fine for this pattern.

Copilot AI requested a review from Tyrrrz July 29, 2026 13:41
Comment thread PowerKit.Tests/ObservableTests.cs
Copilot AI requested a review from Tyrrrz July 29, 2026 13:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants