Skip to content

Commit 08c11e4

Browse files
committed
fix(extractor): bind the EventSource-counter exemption to genuine counter fields (Codex P2 + CodeRabbit)
Two review findings on the over-broad first cut, both the over-suppression class: - Codex P2 — require the field's DECLARED type to derive from DiagnosticCounter before skipping. A field declared as a plain IDisposable that is merely assigned a counter once (`IDisposable _mixed = new MemoryStream(); _mixed = new EventCounter(..., this);`) would otherwise have its OTHER resource (the MemoryStream) silently suppressed by the name-only skip. The declared-type guard binds the exemption to actual counter fields (Npgsql declares them as `IncrementingPollingCounter?` etc.), so the non-counter leak still fires. - CodeRabbit (major) — populate the exemption set with ThisFieldName, not FieldName: the latter also matches `other._counter`, which could exempt this class's same-named field from another instance's assignment. Tightening a suppression to THIS instance's field (`_f` / `this._f`) is strictly safer. Regression: EventSourceCountersSample gains `_mixed` — declared IDisposable, initialized `new MemoryStream()` and later assigned `new EventCounter(..., this)` — which must STILL raise OWN001 on the MemoryStream. The four genuine counter fields stay SILENT; `_scratch` (non-counter) and `_mixed` (non-counter-declared) both warn. CI asserts all three. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED
1 parent 72b6927 commit 08c11e4

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ jobs:
343343
# EventSource (`_scratch`) must STILL raise the OWN001 disposable-field leak.
344344
echo "$out" | grep -qE "EventSourceCountersSample\.cs:[0-9]+:.*\[OWN001\].*'_scratch'" \
345345
|| { echo "FAIL: a non-counter owned IDisposable field in an EventSource must still warn (exemption stays counter-type-scoped)"; exit 1; }
346+
# declared-type control (Codex): a field DECLARED as a plain IDisposable that is ALSO
347+
# assigned a counter once (`_mixed = new EventCounter(..., this)`) must STILL leak its
348+
# earlier `new MemoryStream()` — the exemption requires the DECLARED field type to be a
349+
# DiagnosticCounter, so a name-only skip cannot hide the non-counter resource.
350+
echo "$out" | grep -qE "EventSourceCountersSample\.cs:[0-9]+:.*\[OWN001\].*'_mixed'" \
351+
|| { echo "FAIL: a field declared as a non-counter IDisposable that is later assigned a counter must still leak (exemption requires the DECLARED type to be a DiagnosticCounter)"; exit 1; }
346352
# P-004 process-lived-subscriber exemption (mined: ScreenToGif App +
347353
# Translator): the WPF `App` singleton hooking the process-lived
348354
# AppDomain.UnhandledException promotes nothing, so the static-source region

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ or ImplicitObjectCreationExpressionSyntax
21982198
if (a.IsKind(SyntaxKind.SimpleAssignmentExpression)
21992199
&& a.Right is BaseObjectCreationExpressionSyntax aoce
22002200
&& IsEventSourceOwnedCounter(aoce, model)
2201-
&& FieldName(a.Left) is { } cfn)
2201+
&& ThisFieldName(a.Left) is { } cfn) // THIS instance's field only — `other._c` must not exempt our field (CodeRabbit)
22022202
eventSourceCounters.Add(cfn);
22032203

22042204
foreach (var fd in cls.Members.OfType<FieldDeclarationSyntax>())
@@ -2219,9 +2219,14 @@ or ImplicitObjectCreationExpressionSyntax
22192219
{
22202220
if (!constructed.Contains(v.Identifier.Text))
22212221
continue;
2222-
// an EventSource's own DiagnosticCounter (registered to `this`) is process-lived
2223-
// and never field-disposed -> not an owned leak (mined: Npgsql NpgsqlEventSource).
2224-
if (eventSourceCounters.Contains(v.Identifier.Text))
2222+
// an EventSource's own DiagnosticCounter (a field DECLARED as a DiagnosticCounter
2223+
// AND registered to `this`) is process-lived and never field-disposed -> not an
2224+
// owned leak (mined: Npgsql NpgsqlEventSource). The DECLARED-type guard keeps the
2225+
// skip bound to genuine counter fields: a field declared as a plain IDisposable
2226+
// that is merely assigned a counter once still leaks its OTHER resource (e.g. an
2227+
// initial `new MemoryStream()`), so it must NOT be suppressed by name alone (Codex).
2228+
if (eventSourceCounters.Contains(v.Identifier.Text)
2229+
&& DerivesFromDiagnosticCounter(model.GetTypeInfo(fd.Declaration.Type).Type))
22252230
continue;
22262231
subs.Add(new
22272232
{

‎frontend/roslyn/samples/EventSourceCountersSample.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.Tracing;
3+
using System.IO;
34
using System.Threading;
45

56
namespace Own.Samples;
@@ -32,6 +33,12 @@ internal sealed class SampleEventSource : EventSource
3233
// so this new'd-but-never-disposed resource STILL raises OWN001 ('disposable field').
3334
private readonly OwnedScratch _scratch = new OwnedScratch();
3435

36+
// Codex control: a field DECLARED as a plain IDisposable that is ALSO assigned a counter once
37+
// must STILL leak. The exemption requires the DECLARED field type to be a DiagnosticCounter, so
38+
// the `new MemoryStream()` owned here is not hidden by the later `_mixed = new EventCounter(...)`
39+
// (a name-only skip would wrongly suppress the whole field). -> OWN001 on the MemoryStream.
40+
private IDisposable _mixed = new MemoryStream();
41+
3542
private SampleEventSource() { }
3643

3744
public void RecordBytes(long n) => Interlocked.Add(ref _bytesWritten, n);
@@ -59,6 +66,11 @@ protected override void OnEventCommand(EventCommandEventArgs command)
5966
{
6067
DisplayName = "Total Commands",
6168
};
69+
70+
// The `_mixed` field (declared IDisposable) is ALSO assigned a counter here. The exemption
71+
// must NOT suppress it: its declared type is not a DiagnosticCounter, so its earlier
72+
// `new MemoryStream()` is a real undisposed leak (Codex).
73+
_mixed = new EventCounter("mixed-counter", this);
6274
}
6375
}
6476

0 commit comments

Comments
 (0)