@@ -483,6 +483,40 @@ static bool DerivesFromWinFormsForm(ITypeSymbol? t)
483483 return false ;
484484}
485485
486+ // A type that derives from System.Diagnostics.Tracing.EventSource (semantic, walks the
487+ // base chain). An EventSource is the canonical process-lived `static readonly Log`
488+ // diagnostics singleton; its DiagnosticCounter fields are owned by it (IsEventSourceOwnedCounter).
489+ static bool DerivesFromEventSource ( ITypeSymbol ? t )
490+ {
491+ for ( var b = t ; b is not null ; b = b . BaseType )
492+ if ( b . Name == "EventSource" && b . ContainingNamespace ? . ToString ( ) == "System.Diagnostics.Tracing" )
493+ return true ;
494+ return false ;
495+ }
496+
497+ // A type that derives from System.Diagnostics.Tracing.DiagnosticCounter — the abstract base of
498+ // EventCounter / IncrementingEventCounter / PollingCounter / IncrementingPollingCounter.
499+ static bool DerivesFromDiagnosticCounter ( ITypeSymbol ? t )
500+ {
501+ for ( var b = t ; b is not null ; b = b . BaseType )
502+ if ( b . Name == "DiagnosticCounter" && b . ContainingNamespace ? . ToString ( ) == "System.Diagnostics.Tracing" )
503+ return true ;
504+ return false ;
505+ }
506+
507+ // P-004 EventSource-owned diagnostic counter. A DiagnosticCounter created with `this` — the
508+ // parent EventSource — as its owner argument REGISTERS the counter with that source: the
509+ // runtime's CounterGroup pins it to the EventSource's lifetime, and an EventSource is a
510+ // process-lived `static readonly Log` singleton, so the counter is a process-lived diagnostic
511+ // that is idiomatically NEVER field-disposed (every BCL EventSource — RuntimeEventSource, the
512+ // ASP.NET / HttpConnection counters — does exactly this). Reporting such a field as an
513+ // undisposed leak is therefore a false positive. Mined: Npgsql's NpgsqlEventSource (eight
514+ // counters built in OnEventCommand, none field-disposed).
515+ static bool IsEventSourceOwnedCounter ( BaseObjectCreationExpressionSyntax oce , SemanticModel model ) =>
516+ oce . ArgumentList is { } args
517+ && args . Arguments . Any ( arg => arg . Expression is ThisExpressionSyntax )
518+ && DerivesFromDiagnosticCounter ( model . GetTypeInfo ( oce ) . Type ) ;
519+
486520static string MethodName ( BaseMethodDeclarationSyntax m ) => m switch
487521{
488522 MethodDeclarationSyntax md => md . Identifier . Text ,
@@ -2215,6 +2249,21 @@ or ImplicitObjectCreationExpressionSyntax
22152249 && mb . Name . Identifier . Text is "Dispose" or "DisposeAsync" )
22162250 disposed . Add ( cdf ) ;
22172251
2252+ // P-004 EventSource counter exemption: inside an EventSource, a DiagnosticCounter field
2253+ // constructed with `this` is registered to (and lifetime-owned by) the source — a
2254+ // process-lived diagnostic the source never field-disposes (see IsEventSourceOwnedCounter).
2255+ // Collect those field names so the loop below does not report them as undisposed leaks.
2256+ // A counter is always built in a method body (OnEventCommand) — `this` is unavailable in
2257+ // a field initializer — so only the assignment shape (matching `constructed`) can match.
2258+ var eventSourceCounters = new HashSet < string > ( ) ;
2259+ if ( DerivesFromEventSource ( clsSymbol ) )
2260+ foreach ( var a in assigns )
2261+ if ( a . IsKind ( SyntaxKind . SimpleAssignmentExpression )
2262+ && a . Right is BaseObjectCreationExpressionSyntax aoce
2263+ && IsEventSourceOwnedCounter ( aoce , model )
2264+ && ThisFieldName ( a . Left ) is { } cfn ) // THIS instance's field only — `other._c` must not exempt our field (CodeRabbit)
2265+ eventSourceCounters . Add ( cfn ) ;
2266+
22182267 foreach ( var fd in cls . Members . OfType < FieldDeclarationSyntax > ( ) )
22192268 {
22202269 // a `static` IDisposable field is a process-lifetime singleton (a shared
@@ -2233,6 +2282,15 @@ or ImplicitObjectCreationExpressionSyntax
22332282 {
22342283 if ( ! constructed . Contains ( v . Identifier . Text ) )
22352284 continue ;
2285+ // an EventSource's own DiagnosticCounter (a field DECLARED as a DiagnosticCounter
2286+ // AND registered to `this`) is process-lived and never field-disposed -> not an
2287+ // owned leak (mined: Npgsql NpgsqlEventSource). The DECLARED-type guard keeps the
2288+ // skip bound to genuine counter fields: a field declared as a plain IDisposable
2289+ // that is merely assigned a counter once still leaks its OTHER resource (e.g. an
2290+ // initial `new MemoryStream()`), so it must NOT be suppressed by name alone (Codex).
2291+ if ( eventSourceCounters . Contains ( v . Identifier . Text )
2292+ && DerivesFromDiagnosticCounter ( model . GetTypeInfo ( fd . Declaration . Type ) . Type ) )
2293+ continue ;
22362294 subs . Add ( new
22372295 {
22382296 @event = v . Identifier . Text ,
0 commit comments