Skip to content

Commit 0cc0d5b

Browse files
committed
fix(extractor): make IsStaticHandler resolve member-group symbols; drop static-class escape suppression (Codex)
Codex P2: blanket-suppressing OWN014 for ALL static-source subscriptions in a static class is unsound — a CAPTURING lambda still retains its compiler-generated display-class instance for the process, even with no `this`. Codex's pointer is right: the mined case (MemoryAllocatorValidator) uses static-METHOD handlers, which the existing IsStaticHandler is meant to exempt; it missed them because a method group's symbol can surface as a member group (Symbol == null, CandidateSymbols populated). So: make IsStaticHandler fall back to CandidateSymbols (requiring ALL candidates static, so a mixed overload set is not wrongly exempted), and revert the static-class suppression entirely. A static-method handler in a static class is now exempted by IsStaticHandler (null target, no instance), while a capturing lambda there still escapes — sound. Sample now uses a static-METHOD handler on a static event from a static class's static ctor (replicating MemoryAllocatorValidator) and must stay silent; StaticEventEscapeViewModel still proves an instance handler on a static event escapes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 8e80d66 commit 0cc0d5b

3 files changed

Lines changed: 38 additions & 31 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,13 @@ jobs:
322322
if echo "$out" | grep -q "CleanStaticEventViewModel"; then
323323
echo "FAIL: an unsubscribed (released) static-event subscription was wrongly reported"; exit 1
324324
fi
325-
# P-004 static-class escape exemption (mined: ImageSharp MemoryAllocatorValidator): a
326-
# `static class` has no instance, so a static-source subscription (even a lambda) from it
327-
# cannot be a region escape -> OWN014 must NOT fire. (StaticEventEscapeViewModel above
328-
# proves an INSTANCE class on the same shape still escapes, so this stays scoped.)
325+
# P-004 robust static-handler exemption (mined: ImageSharp MemoryAllocatorValidator): a
326+
# static-METHOD handler on a static event stores a null-target delegate -> no instance is
327+
# retained -> OWN014 must NOT fire, even when the method-group symbol surfaces as a member
328+
# group (now resolved via CandidateSymbols). (StaticEventEscapeViewModel above proves an
329+
# INSTANCE handler on the same static event still escapes, so this stays scoped.)
329330
if echo "$out" | grep -q "StaticAllocationCounter"; then
330-
echo "FAIL: a static-source subscription from a static class was wrongly reported as a region escape"; exit 1
331+
echo "FAIL: a static-method handler on a static event was wrongly reported as a region escape"; exit 1
331332
fi
332333
# P-004 process-lived-subscriber exemption (mined: ScreenToGif App +
333334
# Translator): the WPF `App` singleton hooking the process-lived

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,23 @@ BinaryExpressionSyntax b when b.IsKind(SyntaxKind.AsExpression) => b.Left,
185185
// Target is null, so no instance is retained — the subscription cannot leak a
186186
// subscriber, however long-lived the source. Only method-group handlers
187187
// (identifier / member access) are judged; lambdas and delegate-typed values may
188-
// capture state and are left as leak candidates.
189-
static bool IsStaticHandler(ExpressionSyntax right, SemanticModel model) =>
190-
IsHandler(right)
191-
&& model.GetSymbolInfo(right).Symbol is IMethodSymbol { IsStatic: true };
188+
// capture state and are left as leak candidates. A method group's symbol can surface
189+
// as a MEMBER GROUP (Symbol == null, CandidateSymbols populated) instead of the bound
190+
// method, so fall back to the candidates — else a genuinely static-method handler is
191+
// missed and a static-source subscription that retains NO instance is mis-reported as
192+
// a region escape (mined: ImageSharp MemoryAllocatorValidator's static MemoryDiagnostics
193+
// handlers). When falling back, require ALL candidates static so an overload set that
194+
// mixes a static and an instance method is not wrongly exempted.
195+
static bool IsStaticHandler(ExpressionSyntax right, SemanticModel model)
196+
{
197+
if (!IsHandler(right))
198+
return false;
199+
var info = model.GetSymbolInfo(right);
200+
if (info.Symbol is { } s)
201+
return s is IMethodSymbol { IsStatic: true };
202+
var cands = info.CandidateSymbols;
203+
return cands.Length > 0 && cands.All(c => c is IMethodSymbol { IsStatic: true });
204+
}
192205

193206
// P-004 process-lived-subscriber exemption: the WPF application object (`App`) is a
194207
// process-lived singleton — exactly one instance, created at startup, alive until
@@ -2043,11 +2056,6 @@ or ImplicitObjectCreationExpressionSyntax
20432056
// Is this class the process-lived WPF application object? Used to drop the
20442057
// static-source region escape (OWN014) — `App` cannot be over-promoted.
20452058
var clsIsApp = IsProcessLivedApplication(cls);
2046-
// A `static class` has NO instance, so a static-source subscription from it cannot
2047-
// promote an instance to the source's lifetime — the OWN014 escape is vacuous. (Mined:
2048-
// ImageSharp MemoryAllocatorValidator, a static class whose static ctor hooks the static
2049-
// MemoryDiagnostics events.) Drops only the static-source escape, not OWN001 token leaks.
2050-
var clsIsStaticClass = cls.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword));
20512059

20522060
var subs = new List<object>();
20532061
foreach (var a in assigns)
@@ -2081,13 +2089,12 @@ or ImplicitObjectCreationExpressionSyntax
20812089
: SubscriptionSourceKind(a.Left, ev, model);
20822090
if (source == "local")
20832091
continue;
2084-
// A static-source subscription whose SUBSCRIBER cannot be over-promoted is not a
2085-
// region escape (OWN014): the process-lived WPF `App` singleton (its lifetime
2086-
// already equals the process), or a `static class` (no instance exists at all —
2087-
// mined: ImageSharp MemoryAllocatorValidator). Scoped to NON-timers: a timer is
2088-
// forced to source "static" above, but a never-stopped timer is still a real
2089-
// leak (CodeRabbit).
2090-
if (!isTimer && source == "static" && (clsIsApp || clsIsStaticClass))
2092+
// Process-lived subscriber (the WPF `App` singleton): a static-source
2093+
// subscription promotes nothing — `App` already lives for the whole
2094+
// process — so the region escape (OWN014) is a false positive. Scoped
2095+
// to NON-timers: a timer is forced to source "static" above, but a
2096+
// never-stopped timer in `App` is still a real leak (CodeRabbit).
2097+
if (!isTimer && source == "static" && clsIsApp)
20912098
continue;
20922099
var released = unsub.Contains($"{a.Left}|{a.Right}")
20932100
|| (isTimer && Receiver(a.Left) is { } recv && stopped.Contains(recv));

‎frontend/roslyn/samples/StaticClassEscapeSample.cs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Own.Samples;
44

5-
// P-004 static-class region-escape exemption (mined: ImageSharp MemoryAllocatorValidator).
5+
// P-004 static-handler exemption, robustly (mined: ImageSharp MemoryAllocatorValidator).
66
//
7-
// A `static class` has NO instance, so subscribing to a process-lived STATIC event from its
8-
// static ctor cannot promote an instance to the source's lifetime — OWN014 must NOT fire. A
9-
// LAMBDA handler is used on purpose: it is NOT covered by the static-method-handler exemption,
10-
// so silence here exercises the static-class drop itself, not that exemption. Contrast:
11-
// StaticEventEscapeViewModel (an INSTANCE class on the same shape) must STILL raise OWN014.
7+
// A static class whose static ctor hooks a process-lived STATIC event with a STATIC METHOD
8+
// handler stores a delegate whose Target is null — no instance is retained, so OWN014 must NOT
9+
// fire. The mined case slipped through because the method-group symbol can surface as a member
10+
// group (Symbol == null), which IsStaticHandler now resolves via CandidateSymbols. Contrast:
11+
// StaticEventEscapeViewModel — an INSTANCE handler on a static event — must still raise OWN014
12+
// (capturing lambdas in a static class likewise still escape: the closure is retained).
1213

1314
public static class StaticDiagnosticsBus
1415
{
@@ -19,12 +20,10 @@ public static class StaticDiagnosticsBus
1920

2021
public static class StaticAllocationCounter
2122
{
22-
private static int count;
23-
2423
static StaticAllocationCounter()
2524
{
26-
StaticDiagnosticsBus.Allocated += (_, _) => count++; // lambda + static event + static class -> SILENT
25+
StaticDiagnosticsBus.Allocated += OnAllocated; // static-method handler -> null target -> SILENT
2726
}
2827

29-
public static int Count => count;
28+
private static void OnAllocated(object? sender, EventArgs e) { }
3029
}

0 commit comments

Comments
 (0)