Skip to content

Commit 467cb27

Browse files
authored
Merge pull request #147 from PhysShell/claude/own-net-tasks-3k6d07
oracle: fix dotted-test-dir filter + dispose-optional StringWriter/StringReader (+ field notes)
2 parents 3703b05 + f799ca0 commit 467cb27

5 files changed

Lines changed: 158 additions & 15 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ jobs:
257257
if echo "$out" | grep -q "HolderWithDisposeOptional"; then
258258
echo "FAIL: a dispose-optional (Task/DataTable) field was wrongly flagged"; exit 1
259259
fi
260+
# the same rule for string-backed reader/writer fields (field-notes #8, Newtonsoft
261+
# TraceJsonReader/Writer): a new'd, undisposed StringWriter/StringReader holds no
262+
# unmanaged resource -> must stay SILENT (IsDisposeOptional, System.IO).
263+
if echo "$out" | grep -q "HolderWithStringWriter"; then
264+
echo "FAIL: a dispose-optional (StringWriter/StringReader) field was wrongly flagged"; exit 1
265+
fi
260266
# field release recognition (mined: ImageSharp). #2 null-conditional dispose
261267
# `field?.Dispose()` must be recognized -> silent; the undisposed control still warns.
262268
if echo "$out" | grep -q "DisposesViaConditional"; then

‎docs/notes/field-notes-patterns.md‎

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,110 @@ not luck — and the next recall lever for this family is the deferred field-sto
214214
anything in step 2's scope. *(Confidence: high — dispositions verified against the pinned
215215
Polly 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
222305
responsibility travels with the reference** — out of a factory (1, 6), forward in
223306
time via a callback (2), into a pool (3), or down a `using` on a value type (4).
224307
Naive "every disposable needs a lexical `using`/`Dispose` on every path" checks
225308
misread 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

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,14 @@ static bool IsDisposeOptional(ITypeSymbol t)
727727
{
728728
var ns = t.ContainingNamespace?.ToString();
729729
return (ns == "System.Threading.Tasks" && t.Name is "Task" or "ValueTask")
730-
|| (ns == "System.Data" && t.Name is "DataTable" or "DataSet" or "DataView");
730+
|| (ns == "System.Data" && t.Name is "DataTable" or "DataSet" or "DataView")
731+
// System.IO string-backed reader/writer: a StringWriter wraps a StringBuilder
732+
// and a StringReader reads a string — neither holds an OS handle / unmanaged
733+
// resource, so Dispose() frees nothing real and an undisposed one is not a leak.
734+
// (field-notes #8, mined on Newtonsoft.Json's TraceJsonReader/TraceJsonWriter,
735+
// where these are owning fields the trace helpers never dispose.) MemoryStream is
736+
// deliberately NOT here — it can own a real buffer, so it still warns.
737+
|| (ns == "System.IO" && t.Name is "StringWriter" or "StringReader");
731738
}
732739

733740
// A type that is System.Windows.Forms.Form or derives from it (semantic, walks the

‎frontend/roslyn/samples/ResolvedDisposableSample.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ public sealed class HolderWithDisposeOptional
5252

5353
public int Use() => this.task.Id + this.table.Columns.Count;
5454
}
55+
56+
// String-backed reader/writer dispose-optional control (field-notes #8, mined on
57+
// Newtonsoft.Json's TraceJsonReader/TraceJsonWriter): a StringWriter wraps a
58+
// StringBuilder and a StringReader reads a string — no unmanaged resource, so Dispose()
59+
// frees nothing real. A new'd, undisposed field of these must stay SILENT (IsDisposeOptional).
60+
// Contrast HolderWithRealDisposable's MemoryStream above, which still warns.
61+
public sealed class HolderWithStringWriter
62+
{
63+
private readonly StringWriter writer = new();
64+
private readonly StringReader reader = new("x");
65+
66+
public string Use() => this.writer.ToString() + this.reader.ReadToEnd();
67+
}

‎scripts/oracle_compare.py‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,24 @@ def _is_test_path(path: str) -> bool:
378378
(e.g. Polly's `src/Snippets/Docs/*`, where ~20 `HttpResponseMessage`/`HttpClient`
379379
examples are never disposed by design) — counting them as product leaks inflates
380380
the oracle-only recall gap with example code that was never meant to dispose."""
381-
for seg in path.lower().split("/"):
382-
# Exact match for short, collision-prone names — a `snippet` *prefix* would
383-
# wrongly drop product dirs like `SnippetEngine`/`SnippetService` (Polly's
384-
# `src/Snippets/Docs/*` is caught by the exact `snippets`/`docs` segments
385-
# anyway). Prefix match only for the long, unambiguous plural-able ones.
386-
if (seg in ("test", "tests", "doc", "docs", "snippet", "snippets")
387-
or seg.startswith(("benchmark", "sample", "example"))):
388-
return True
381+
# Only DIRECTORY segments classify a finding as non-product — NEVER the file's own
382+
# name (the last segment). A product file literally named `Test.cs` / `Doc.cs` /
383+
# `Foo.Tests.cs` is real product code and must NOT be dropped from the diff, or we
384+
# under-count findings (Codex). So iterate the directory segments only (`segs[:-1]`).
385+
segs = path.lower().split("/")
386+
for seg in segs[:-1]:
387+
# Split each directory segment on '.' before matching, for the ubiquitous .NET
388+
# `<Project>.Tests` / `Foo.Benchmarks` directory convention: the dir
389+
# `Newtonsoft.Json.Tests` is ONE path segment, so a bare-segment check
390+
# misses it (and silently drags ~485 test findings into the diff). Matching
391+
# each dot-component catches the `tests` tail while keeping the original
392+
# guards intact — exact match for short, collision-prone names (so a single
393+
# component `SnippetEngine`/`Documentation` is NOT dropped, only an exact
394+
# `snippet`/`doc`), prefix only for the long, unambiguous plural-able ones.
395+
for part in seg.split("."):
396+
if (part in ("test", "tests", "doc", "docs", "snippet", "snippets")
397+
or part.startswith(("benchmark", "sample", "example"))):
398+
return True
389399
return False
390400

391401

@@ -560,10 +570,24 @@ def _selftest() -> int:
560570
("tests/Foo/Bar.cs", "benchmarks/X/Y.cs", "src/Test/Z.cs",
561571
"src/Snippets/Docs/Fallback.cs", "src/MyLib/docs/Example.cs")):
562572
fails.append("_is_test_path should match test/benchmark/doc/snippet trees")
573+
# The .NET `<Project>.Tests` / `Foo.Benchmarks` convention: the dotted dir is a
574+
# single path segment, so it must be matched on its dot-components — else a repo
575+
# like Newtonsoft.Json (test tree `Newtonsoft.Json.Tests`) leaks ~485 test
576+
# findings into the product diff (own-only 489 vs the true 4). The sibling
577+
# product dir `Newtonsoft.Json` must stay IN scope.
578+
if not all(_is_test_path(p) for p in
579+
("Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs",
580+
"src/Foo.Benchmarks/Bench.cs")):
581+
fails.append("_is_test_path should match dotted .NET test/benchmark project dirs")
563582
# ...but a product dir whose name merely *starts with* a marker word is NOT
564-
# excluded — exact match for snippet/doc guards against dropping real code.
583+
# excluded — exact match for snippet/doc guards against dropping real code, and
584+
# the dot-split must leave a non-test dotted product namespace untouched. Critically,
585+
# the dot-split must NOT look at the FILE name: a product file literally named
586+
# `Test.cs` / `Doc.cs` / `Foo.Tests.cs` is real code, not a test tree (Codex).
565587
if any(_is_test_path(p) for p in ("Dapper/SqlMapper.cs", "src/Lib/A.cs",
566-
"src/SnippetEngine/Foo.cs", "src/Documentation/Api.cs")):
588+
"src/SnippetEngine/Foo.cs", "src/Documentation/Api.cs",
589+
"Src/Newtonsoft.Json/JsonSerializer.cs",
590+
"src/Test.cs", "src/Doc.cs", "src/Foo.Tests.cs")):
567591
fails.append("_is_test_path should not match product paths")
568592
# the scope note is gated on mode, not count: a product-only run that excluded
569593
# nothing must still say so (else it reads like a full-scope run).
@@ -607,7 +631,7 @@ def _selftest() -> int:
607631
fails.append(f"non-SARIF JSON masked as clean: {len(not_sarif)} findings, "
608632
f"{ns_drift} unparsed")
609633

610-
total = 24
634+
total = 25
611635
for f in fails:
612636
print(f"ORACLE SELFTEST FAIL: {f}")
613637
print(f"oracle_compare selftest: {total - len(fails)}/{total} checks passed")

0 commit comments

Comments
 (0)