|
| 1 | +# Subscription released via explicit-delegate `+=` / bare `-=` (release-match FP) |
| 2 | + |
| 3 | +**Pattern.** A view holds an injected `INotifyPropertyChanged` it does not own, |
| 4 | +subscribes in the ctor, and unsubscribes in `Dispose`. The codebase's idiom writes |
| 5 | +the two sides **asymmetrically**: |
| 6 | + |
| 7 | +```csharp |
| 8 | +_source.PropertyChanged += new PropertyChangedEventHandler(OnSourcePropertyChanged); // ctor: explicit delegate-creation |
| 9 | +_source.PropertyChanged -= OnSourcePropertyChanged; // Dispose: bare method group |
| 10 | +``` |
| 11 | + |
| 12 | +Both name the same `(receiver, handler)` pair; the only difference is the `+=` |
| 13 | +wraps the handler in a `new …Handler(...)` delegate-creation and the `-=` does not. |
| 14 | +The explicit-delegate `+=` is the dominant subscription shape in SectorTS |
| 15 | +(`BrokerDataClasses/*.cs`, ~250 sites). |
| 16 | + |
| 17 | +**The bug (Own.NET extractor).** The Roslyn extractor keys release on the raw |
| 18 | +handler *text*: `+=` records `new System.ComponentModel.PropertyChangedEventHandler(H)` |
| 19 | +as the handler and the `-=` collector stores the bare `H`, so |
| 20 | +`unsub.Contains("{left}|{right}")` never matches. Worse, `IsHandler` accepts only |
| 21 | +`IdentifierName` / `MemberAccess`, so a `-= new …Handler(H)` is not even collected, |
| 22 | +and the P-004 static-handler exemption is skipped for a `+= new …Handler(StaticM)`. |
| 23 | +Net effect: a correctly torn-down subscription is reported as an unreleased leak — |
| 24 | +a false positive. The fix normalizes a delegate-creation to its inner handler |
| 25 | +before keying (extractor `NormalizeHandler`), so the wrapped `+=` and the bare `-=` |
| 26 | +match. |
| 27 | + |
| 28 | +**Why this case uses a single-source `Dispose` (not a rebinding setter).** The |
| 29 | +`after.cs` here holds a **readonly** `_source` set once in the ctor and released in |
| 30 | +`Dispose`, so "after = clean" is **unconditionally** true: the one subscription |
| 31 | +created is the exact one torn down. It deliberately avoids the *setter-rebind* |
| 32 | +idiom (`… -= h; _source = value; _source.PropertyChanged += new …(h)`), because |
| 33 | +own-check's release model is **not flow-sensitive** — it treats *any* matching `-=` |
| 34 | +in the class as releasing the subscription. Under a rebinding setter that model |
| 35 | +would call the subscription released even though the `-=` detached only the *old* |
| 36 | +source and the newly-assigned source is never torn down (Codex P2 on #163). That |
| 37 | +soundness gap is **pre-existing** (a bare `-=`/`+=` rebinding setter already |
| 38 | +behaved identically before this PR) and orthogonal to this text-normalization fix; |
| 39 | +tracking the last-rebound subscription would need flow-sensitive release analysis, |
| 40 | +a separate change. Keeping the corpus case rebind-free makes the regression assert |
| 41 | +the normalization only, not the lenient model. |
| 42 | + |
| 43 | +**What the checker says (`.own` reduction).** Modelling the ctor+Dispose as one |
| 44 | +scope, the unreleased token in `before.cs` is the generic **OWN001** (owned |
| 45 | +resource not released on all paths), carrying the resource-kind tag: |
| 46 | + |
| 47 | +```text |
| 48 | +$ python -m ownlang check corpus/wpf/subscription-explicit-delegate-release/case.own |
| 49 | +case.own:…: error: [OWN001] 'sub' is owned but not released at end of function |
| 50 | + [resource: subscription token] |
| 51 | +``` |
| 52 | + |
| 53 | +**Regression guard.** `scripts/benchmark.py` runs the real C# through the |
| 54 | +extractor + core: `before.cs` must be **caught** (the leak is real) and `after.cs` |
| 55 | +must be **silent** (the release is recognized). Before the `NormalizeHandler` fix, |
| 56 | +`after.cs` falsely reports OWN001 → the benchmark's specificity gate fails; after |
| 57 | +it, silent. |
| 58 | + |
| 59 | +**Honesty / scope.** `case.own` is a hand reduction that carries only the |
| 60 | +acquire/release logic; the syntactic `+=`/`-=` asymmetry that the extractor must |
| 61 | +normalize lives in `before.cs` / `after.cs`, which are representative of the real |
| 62 | +SectorTS idiom, not a verbatim copy of one file. |
0 commit comments