@@ -214,17 +214,110 @@ not luck — and the next recall lever for this family is the deferred field-sto
214214anything in step 2's scope. * (Confidence: high — dispositions verified against the pinned
215215Polly source, not just the SARIF excerpts.)*
216216
217+ ## 8. Event subscription on a freshly-created, * returned* publisher
218+
219+ ** Seen in:** Newtonsoft.Json ` Src/Newtonsoft.Json/JsonSerializer.cs:717 `
220+ (` ApplySerializerSettings ` , commit ` 4f73e74 ` ).
221+
222+ A factory configures a new object and wires an event on it before handing it back;
223+ the subscription is never ` -= ` 'd, but it doesn't need to be.
224+
225+ ``` csharp
226+ public static JsonSerializer Create (JsonSerializerSettings ? settings )
227+ {
228+ JsonSerializer serializer = new JsonSerializer ();
229+ if (settings != null ) ApplySerializerSettings (serializer , settings );
230+ return serializer ; // publisher escapes to the caller
231+ }
232+ private static void ApplySerializerSettings (JsonSerializer serializer , JsonSerializerSettings settings )
233+ {
234+ if (settings .Error != null )
235+ serializer .Error += settings .Error ; // <- flagged: "+= but never -="
236+ }
237+ ```
238+
239+ ** Why:** the * publisher* (` serializer ` ) is the returned object. The handler lives
240+ exactly as long as the serializer the caller now holds; when that is collected, the
241+ subscription dies with it. No ` -= ` is needed — there is no longer-lived source
242+ retaining a shorter-lived target (the dangerous direction our OWN001/OWN014
243+ subscription leak targets). It's the publisher itself that is short/caller-scoped.
244+
245+ ** Analyzer angle — this is an * own-only* over-report (the first in this notebook
246+ where Own.NET, not the oracle, is too strict).** CodeQL/Infer# have no
247+ event-subscription-leak query, so they stay silent (correctly). Own.NET fires
248+ because, * inside* ` ApplySerializerSettings ` , ` serializer ` is an opaque ** parameter**
249+ — ` SubscriptionSourceKind ` can't see that the caller ` new ` s it and ` return ` s it, so
250+ it conservatively tiers it ` injected ` and emits a ** warning** (not a hard error;
251+ P-004 severity tiering already hedges unknown-lifetime publishers). So the default
252+ ` error ` posture is unaffected — it only surfaces under ` --severity warning ` (the
253+ oracle's setting). ** Subscription-leak analysis is the dual of ownership transfer:
254+ a ` += ` on a publisher that * escapes by return* is as bounded as a returned
255+ ` IDisposable ` — the fix is the same "follow the reference" escape rule, but
256+ interprocedural (the construct-and-return is in the caller), which is the hard part.
257+ The honest interim posture — advisory warning, never a hard error — is already in
258+ place.**
259+
260+ ## 9. Owning field whose IDisposable holds no unmanaged resource
261+
262+ ** Seen in:** Newtonsoft.Json ` Src/Newtonsoft.Json/Serialization/TraceJsonReader.cs:37,38 `
263+ and ` TraceJsonWriter.cs:39 ` (commit ` 4f73e74 ` ).
264+
265+ A type owns a disposable field but never disposes it — and that is fine, because the
266+ field's ` Dispose() ` frees nothing real.
267+
268+ ``` csharp
269+ internal class TraceJsonReader : JsonReader // JsonReader : IDisposable, no Dispose override
270+ {
271+ private readonly StringWriter _sw ; // StringBuilder-backed: Dispose() is a no-op
272+ private readonly JsonTextWriter _textWriter ; // wraps _sw; at most returns a pooled char buffer
273+ public TraceJsonReader (JsonReader inner )
274+ {
275+ _sw = new StringWriter (CultureInfo .InvariantCulture );
276+ _textWriter = new JsonTextWriter (_sw );
277+ }
278+ }
279+ ```
280+
281+ ** Why:** these are short-lived, per-call diagnostic helpers (created only when a
282+ ` TraceWriter ` is attached at ` Verbose ` ) that capture the JSON text into an in-memory
283+ ` StringWriter ` . A ` StringWriter ` wraps a ` StringBuilder ` — no OS handle, no
284+ unmanaged state; ` Dispose() ` just flips a closed flag. Not disposing it leaks
285+ nothing the GC won't reclaim.
286+
287+ ** Analyzer angle — also an * own-only* over-report.** CodeQL/Infer# stay silent
288+ (they model real resource handles; an undisposed ` StringWriter ` isn't one). Own.NET's
289+ owning-field detector flags every ` IDisposable ` field equally. The lever already
290+ exists: ` IsOwnedDisposableType ` exempts ` IsDisposeOptional ` types (Task/ValueTask/
291+ DataTable/DataSet/DataView — "Dispose is a no-op / only a lazy wait handle"). The
292+ fix for the two ` StringWriter ` fields is to ** add ` System.IO.StringWriter ` /
293+ ` StringReader ` to that exemption** — a one-liner mirroring the existing set. The
294+ third field (` JsonTextWriter ` , a third-party writer over the StringWriter, which on
295+ Dispose returns a pooled char buffer) is not generically exemptable by name and
296+ stays a low-value residual. ** Rule of thumb: "owns an ` IDisposable ` field" is only a
297+ leak when the field owns a * real* resource — a string/in-memory writer is not one,
298+ and the ` IsDisposeOptional ` allowlist is where that knowledge belongs.**
299+
217300---
218301
219302## The through-line
220303
221- Five of six entries are the * same lesson from different angles* : ** disposal
304+ Entries 1–6 are the * same lesson from different angles* : ** disposal
222305responsibility travels with the reference** — out of a factory (1, 6), forward in
223306time via a callback (2), into a pool (3), or down a ` using ` on a value type (4).
224307Naive "every disposable needs a lexical ` using ` /` Dispose ` on every path" checks
225308misread all of them, which is why Infer#/CodeQL over-report here and a
226- transfer/escape-aware checker (Own.NET) correctly stays quiet. Worth learning as
227- C#; worth pinning as the precision frontier.
309+ transfer/escape-aware checker (Own.NET) correctly stays quiet. (Entry 7 is a run
310+ ledger — an audit record of a Polly re-run, not an idiom.)
311+
312+ Entries 8–9 are the ** mirror image — the first cases where _ Own.NET_ is the one
313+ over-reporting and the oracle is correctly silent.** They map our own precision
314+ frontier: a subscription on a publisher that * escapes by return* (8, the dual of
315+ ownership transfer — bounded, but the construct-and-return is interprocedural), and
316+ an owning field whose ` IDisposable ` holds no real resource (9, a ` StringWriter ` is
317+ not a handle — belongs in the ` IsDisposeOptional ` allowlist). Same moral as 1–6,
318+ pointed back at us: ** a leak is about the * resource* and the * reference's
319+ lifetime* , not the mere presence of an ` IDisposable ` and a missing ` Dispose ` /` -= ` .**
320+ Worth learning as C#; worth pinning as the precision frontier.
228321
229322## Maintaining this notebook (a repo requirement)
230323
0 commit comments