Skip to content

Commit 79186a8

Browse files
committed
Extractor: classify self-rooted .Subscribe() chains as source "self"
The extractor half of the WalletWasabi precision win. The ignored-`.Subscribe()` detector (WPF004) now classifies the chain root and stamps `source`: a self-rooted `this.WhenAnyValue(p => p.SelfProp).….Subscribe(...)` is a GC-collectible self-cycle (`source: "self"`, which the bridge drops), while any other root -- an external receiver, `EventBus.Subscribe`, an injected field, or a NESTED path `p => p.A.B` that roots through `A` -- stays unclassified (`source: null`) and remains a flagged leak. `IsSelfRootedWhenAny` is purely syntactic (no SemanticModel): walk the fluent chain to its leftmost invocation and require `this.WhenAnyValue(p => p.Member)` with a single-hop self-member lambda. Conservative by design -- it silences only the unambiguous self-cycle, never an external-source subscription. Safe on the existing sample: `MessengerViewModel`'s `messenger.Subscribe(OnMessage)` has a field receiver (not WhenAnyValue) -> `source: null` -> unchanged OWN001. The bridge tiering that consumes `source` landed in the previous commit; together they take the WalletWasabi mine's 118 raw findings down to the real (external-source) leaks. CI-validated (no local dotnet). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 12cbaf7 commit 79186a8

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

‎frontend/roslyn/OwnSharp.Extractor/Program.cs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,53 @@ static string SubscriptionSourceKind(ExpressionSyntax left, IEventSymbol ev,
235235
static bool IsLambdaHandler(ExpressionSyntax right) =>
236236
right is AnonymousFunctionExpressionSyntax;
237237

238+
// P-004 source-lifetime tier for an ignored `.Subscribe()` chain (WPF004). A
239+
// self-rooted `this.WhenAnyValue(p => p.SelfProp)` watches the component's OWN
240+
// property: the observable, its handler and `this` form one cycle the GC collects
241+
// together, so it is NOT a leak. We classify ONLY this unambiguous self-cycle
242+
// (the bridge drops a `source: "self"` subscribe). Any other root — an external
243+
// receiver, `EventBus.Subscribe`, an injected field, or a NESTED path
244+
// `p => p.A.B` that roots through `A` — stays unclassified so it remains a flagged
245+
// leak (conservative: silence nothing we are not sure about). Purely syntactic.
246+
static bool IsSelfRootedWhenAny(ExpressionSyntax chain)
247+
{
248+
// Walk left down the fluent chain (`.Op(args)` / `.Member`) to the leftmost
249+
// invocation; `root` ends as `<recv>.Method(args)` at the head of the chain.
250+
var e = chain;
251+
InvocationExpressionSyntax? root = null;
252+
while (true)
253+
{
254+
if (e is InvocationExpressionSyntax iv
255+
&& iv.Expression is MemberAccessExpressionSyntax ma)
256+
{
257+
root = iv;
258+
e = ma.Expression;
259+
}
260+
else if (e is MemberAccessExpressionSyntax ma2)
261+
{
262+
e = ma2.Expression;
263+
}
264+
else
265+
{
266+
break;
267+
}
268+
}
269+
// `this.WhenAnyValue(p => p.Member)` — head is WhenAnyValue on `this`, with a
270+
// single-hop self-member lambda (`p => p.Member`, not `p => p.A.B`).
271+
if (root is null
272+
|| root.Expression is not MemberAccessExpressionSyntax head
273+
|| head.Name.Identifier.Text != "WhenAnyValue"
274+
|| head.Expression is not ThisExpressionSyntax
275+
|| root.ArgumentList.Arguments.Count != 1
276+
|| root.ArgumentList.Arguments[0].Expression is not SimpleLambdaExpressionSyntax lam
277+
|| lam.Body is not MemberAccessExpressionSyntax body
278+
|| body.Expression is not IdentifierNameSyntax pid)
279+
{
280+
return false;
281+
}
282+
return pid.Identifier.Text == lam.Parameter.Identifier.Text;
283+
}
284+
238285
// --- P-016 B0b/B2: flow lowering for local IDisposables (experimental) ---
239286

240287
// A type that implements System.IDisposable (semantic) — the flow lowering tracks
@@ -912,6 +959,10 @@ or ImplicitObjectCreationExpressionSyntax
912959
line = LineOf(inv),
913960
released = false,
914961
resource = "subscribe",
962+
// A self-rooted `this.WhenAnyValue(p => p.SelfProp)` chain is a
963+
// GC-collectible self-cycle: the bridge drops `source: "self"`.
964+
// Any other (external) source stays a flagged leak (null source).
965+
source = IsSelfRootedWhenAny(m.Expression) ? "self" : null,
915966
});
916967

917968
// POOL001: an ArrayPool/MemoryPool buffer `Rent`ed but never `Return`ed,

0 commit comments

Comments
 (0)