Skip to content

fix: stop notification delivery after subscription disposal - #252

Merged
glennawatson merged 1 commit into
mainfrom
fix/disposed-subscription-delivery
Nov 21, 2026
Merged

glennawatson merged 1 commit into
mainfrom
fix/disposed-subscription-delivery

Conversation

@glennawatson

@glennawatson glennawatson commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stops notification delivery as soon as a subscription handle is disposed, including when one subscriber synchronously disposes a sibling subscription during an active broadcast.

  • Signal<T> subscriptions now clear their observer and action targets on disposal so captured terminal snapshots neither deliver OnError or OnCompleted nor rethrow for disposed action subscribers.
  • Witness.Subscribe callback overloads now guard callbacks with a disposable witness so disposed or terminated subscriptions ignore later notifications from any IObservable<T>, while internal coordinators keep lightweight EmptyWitness<T> subscriptions under their own state guards.
  • ReplaySignal<T>, BehaviorSignal<T>, and StateSignal<T> now call SerializedWitness<T>.Stop() on subscription disposal (with single-winner detachment in BehaviorWitnessHandler<T>) so serialized delivery halts immediately during an active broadcast.
  • DelayableNotificationSignal<T>, ProjectedReadOnlyState<TSource, TResult>, SliceWindow<T>, and AsyncSignal<T> now detach downstream observers on disposal before an in-flight fan-out reaches them.

Why

When an observable built with Signal.Create faults or completes through SubscribeSafe, the outer sink tears down its subscription resource synchronously before forwarding the terminal signal. Any sibling subscriptions disposed by that cleanup could still receive OnError or OnCompleted from the upstream subject's captured observer array after their own Dispose had already run.

Closes #251

Breaking changes

None

How this was verified

Added unit tests covering Signal.Create sibling teardown on error and completion, mid-broadcast sibling disposal across synchronous signals and windows, post-disposal suppression in Witness.Subscribe, and direct IObserver<T> handle disposal paths, and ran the reproduction program from #251.

Notes for the reviewer

Start with Signal<T>.SignalSubscription and Witness.SubscribeCallbacks, which address the two mechanisms behind #251 directly. The DetachableObserver<TOwner, T> and SerializedWitness<T>.Stop() updates apply the same guarantee across the remaining synchronous fan-out types.

Checklist

  • I have read the Contribute guide
  • The PR title follows Conventional Commits
  • Tests cover this change, or the summary says why they do not
  • New or changed public API has XML documentation

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a5f736e) to head (ee5452f).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #252   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files         1013      1014    +1     
  Lines        27204     27251   +47     
  Branches      3278      3291   +13     
=========================================
+ Hits         27204     27251   +47     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Clear observer and action targets atomically when disposing Signal<T> subscriptions so in-flight terminal snapshots cannot deliver OnError or OnCompleted or rethrow for disposed action subscribers
- Wrap Witness.Subscribe callback subscriptions in a disposable unary witness slot that suppresses post-disposal and duplicate terminal notifications while keeping internal coordinator callback subscriptions on EmptyWitness<T>
- Stop SerializedWitness delivery immediately when ReplaySignal, BehaviorSignal, or StateSignal subscriptions dispose during an active broadcast, and ensure single-winner detachment in BehaviorWitnessHandler<T>
- Share DetachableObserver<TOwner, T> across DelayableNotificationSignal, ProjectedReadOnlyState, SliceWindow, and AsyncSignal so reentrant sibling disposal halts delivery immediately
- Add regression and direct observer-handle tests covering Signal.Create sibling teardown and mid-broadcast subscription disposal
@glennawatson
glennawatson force-pushed the fix/disposed-subscription-delivery branch from 66fbca9 to ee5452f Compare November 21, 2026 06:25
@sonarqubecloud

Copy link
Copy Markdown

@glennawatson
glennawatson merged commit 25d1df7 into main Nov 21, 2026
18 checks passed
@glennawatson
glennawatson deleted the fix/disposed-subscription-delivery branch November 21, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Observers inside Signal.Create() can receive notifications after subscription disposal

1 participant