Skip to content

Commit 12cbaf7

Browse files
committed
Bridge: source-lifetime tiering for subscribe (ignored .Subscribe() result)
The bridge half of the WalletWasabi precision win (118 raw -> the real leaks). The ignored-`.Subscribe()` detector was a flat OWN001 ("always a leak"), but a self-rooted `this.WhenAnyValue(x => x.SelfProp).Subscribe(...)` is a GC-collectible self-cycle (the observable, its handler and `this` form one cycle the GC collects together) -- NOT a leak. Only an EXTERNAL source (EventBus, an injected model/field) holds the component from a longer-lived root. Tier the `subscribe` finding by `source`, mirroring the `+=` else branch: - source == "self" -> dropped in to_module/to_own (silent, no finding) - source == "injected" -> OWN001 WARNING (unknown lifetime, "may outlive") - source == "static"/external/unannotated -> OWN001 leak (error, unchanged) Additive + zero regression: an unannotated `subscribe` (every current fixture) stays an OWN001 error. The extractor half -- classifying the `.Subscribe()` chain root and stamping `source` -- is CI-validated next; this engine is what makes the self-cycles go silent once it does. ownir 77/77, full suite, ruff, mypy --strict. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent feb4e4e commit 12cbaf7

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎ownlang/ownir.py‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
(optional `type` names the field's declared type); tag
3838
`[resource: disposable field]`.
3939
- "subscribe": a `X.Subscribe(...)` whose `IDisposable` result is ignored (a
40-
bare statement, not captured/disposed) — always a leak; tag
40+
bare statement, not captured/disposed). Tiered by `source` like a `+=`: a
41+
self-rooted `this.WhenAnyValue(x => x.SelfProp)` chain (`source: "self"`) is a
42+
GC-collectible self-cycle, not a leak (silent); an `injected` source is a
43+
warning (unknown lifetime); a `static`/external/unknown source is a leak. Tag
4144
`[resource: subscription token]`.
4245
- "capture": a *tokenless* strong subscription (`event += handler` with no
4346
token to release) whose event SOURCE provably outlives the subscriber. This
@@ -425,6 +428,13 @@ def to_own(facts: dict[str, Any]) -> tuple[str, dict[str, dict[str, Any]]]:
425428
# an advisory OWN050 note by _unresolved_findings instead.
426429
if rkind == "unresolved-subscription":
427430
continue
431+
# A `subscribe` (ignored `.Subscribe()` result) whose SOURCE is `self`
432+
# is a self-rooted `this.WhenAnyValue(x => x.SelfProp)` cycle — the
433+
# observable, its handler and `this` form one cycle the GC collects
434+
# together, so it is NOT a leak. Skip it (silent); only an EXTERNAL
435+
# source holds the component from a longer-lived root. Mirrors to_module.
436+
if rkind == "subscribe" and sub.get("source") == "self":
437+
continue
428438
# A `capture` is the tokenless region-escape shape: it does NOT acquire
429439
# a token (no OWN001); it lowers to `subscribe self to <source>` with
430440
# the source's region, so the lifetime engine reports OWN014 when the
@@ -552,6 +562,12 @@ def to_module(facts: dict[str, Any]) -> tuple[Module, dict[str, dict[str, Any]]]
552562
# surfaced as an advisory OWN050 note, never lowered (see to_own).
553563
if rkind == "unresolved-subscription":
554564
continue
565+
# A self-rooted `subscribe` (ignored `.Subscribe()` on a
566+
# `this.WhenAnyValue(x => x.SelfProp)` chain) is a GC-collectible
567+
# self-cycle, not a leak — skip it (silent), like a released
568+
# subscription. Only an EXTERNAL source holds the component.
569+
if rkind == "subscribe" and sub.get("source") == "self":
570+
continue
555571
# P-004 region escape: a `capture` is a tokenless strong subscription
556572
# whose source provably outlives the subscriber. Lower it to the
557573
# lifetime engine (`subscribe self to <source>` + the source's region)
@@ -932,9 +948,21 @@ def check_facts(facts: dict[str, Any]) -> list[Finding]:
932948
message = (f"local IDisposable '{event}'{of_type} is created but "
933949
f"never disposed (leak)")
934950
elif rkind == "subscribe":
935-
message = (f"the result of '{event}' is ignored — the IDisposable "
936-
f"subscription is never disposed, leaking "
937-
f"'{component}' (leak)")
951+
# source-lifetime tiering (P-004), mirroring the `+=` else branch: a
952+
# self-rooted subscribe is already dropped in to_module (silent); an
953+
# injected source has UNKNOWN lifetime -> warning ("may outlive"); a
954+
# static/external/unknown source stays a provable leak (error).
955+
if sub.get("source") == "injected":
956+
fsev = "warning"
957+
message = (f"the result of '{event}' is ignored — its IDisposable "
958+
f"subscription is never disposed; the source is an "
959+
f"injected dependency whose lifetime is unknown, so it "
960+
f"may outlive and keep '{component}' alive (possible "
961+
f"leak)")
962+
else:
963+
message = (f"the result of '{event}' is ignored — the IDisposable "
964+
f"subscription is never disposed, leaking "
965+
f"'{component}' (leak)")
938966
elif rkind == "pool":
939967
message = (f"pooled buffer '{event}' is rented but never returned "
940968
f"to the pool (leak)")

‎tests/test_ownir.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,41 @@ def _one(source: str, lambda_: bool = False) -> Finding:
170170
if "inline lambda" not in lam.message or "-=" not in lam.message:
171171
fails.append(f"lambda handler should note the missing -= handle: {lam.message!r}")
172172

173+
# --- P-004 source-lifetime tiering for `subscribe` (ignored `.Subscribe()`
174+
# result) — the WalletWasabi precision win. A SELF-rooted subscribe
175+
# (`this.WhenAnyValue(x => x.SelfProp)`) is a GC-collectible self-cycle ->
176+
# silent (not a leak); an `injected` source -> warning (unknown lifetime);
177+
# a `static`/external/unannotated source -> a leak (error). Mirrors `+=`.
178+
def _sub(source: str | None) -> list[Finding]:
179+
s: dict[str, object] = {
180+
"event": "this.WhenAnyValue(x => x.Foo).Subscribe", "handler": "",
181+
"line": 7, "released": False, "resource": "subscribe"}
182+
if source is not None:
183+
s["source"] = source
184+
return check_facts({"module": "M", "components": [
185+
{"name": "Vm", "file": "Vm.cs", "subscriptions": [s]}]})
186+
187+
# self-rooted -> silent (the cycle the GC collects); the 118->real win.
188+
checks += 1
189+
if _sub("self"):
190+
fails.append(f"a self-rooted subscribe must be silent (self-cycle), got "
191+
f"{[(x.code, x.severity) for x in _sub('self')]}")
192+
# injected source -> OWN001 WARNING (unknown lifetime, may outlive).
193+
checks += 1
194+
si = _sub("injected")
195+
if [(x.code, x.severity) for x in si] != [("OWN001", "warning")]:
196+
fails.append(f"injected subscribe should be an OWN001 warning, got "
197+
f"{[(x.code, x.severity) for x in si]}")
198+
elif "may outlive" not in si[0].message:
199+
fails.append(f"injected subscribe message missing wording: {si[0].message!r}")
200+
# external (static) and UNANNOTATED -> OWN001 error (unchanged — no regression).
201+
checks += 1
202+
for src in ("static", None):
203+
se = _sub(src)
204+
if [(x.code, x.severity) for x in se] != [("OWN001", None)]:
205+
fails.append(f"subscribe source={src!r} should stay an OWN001 error, "
206+
f"got {[(x.code, x.severity) for x in se]}")
207+
173208
# the fixture carries the current schema version (the contract is stamped).
174209
checks += 1
175210
if facts.get("ownir_version") != OWNIR_VERSION:

0 commit comments

Comments
 (0)