Skip to content

Commit 963af37

Browse files
committed
feat(p1a): recognise ADO.NET owned-returning members (ExecuteReader/CreateCommand/BeginTransaction)
P1a ADO.NET tranche — the single most common real-world .NET resource leak: a DbDataReader from `cmd.ExecuteReader()` (or a DbCommand from `CreateCommand()`, a DbTransaction from `BeginTransaction()`) dropped without Dispose holds the reader / command / server-side cursor open until finalization. These are INSTANCE methods on a connection/command, unlike the existing static File/crypto/Xml/Json factories — but the acquire path (op="acquire") is static-vs-instance agnostic and the receiver is not an argument, so nothing is dropped (spike-confirmed). Recognise them in `IsOwningFactory` by method name + the RESOLVED return type implementing the System.Data contract interface (IDataReader / IDbCommand / IDbTransaction), which is provider-agnostic: it covers every concrete provider (SqlDataReader, NpgsqlDataReader, …), the abstract base (DbDataReader/DbCommand/DbTransaction), and the interface itself. Guarded by !AnyDisposableArgument (args are CommandBehavior/IsolationLevel enums anyway). Corpus fixture `ado-executereader-leak`: before.cs leaks the reader (OWN001), after.cs disposes it with `using` (clean), case.own reduces the pattern. Locally verified: case.own → OWN001 (test_corpus 21/21), full suite green, mypy --strict, ruff. The C# extractor branch and the before.cs/after.cs end-to-end run are verified by CI (no dotnet locally): golden C# compiles, C# leak extractor, and the corpus benchmark (before caught + after clean, no specificity regression). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019846YSZ35c7CdkWQ1qX5gm
1 parent 83edff6 commit 963af37

6 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Data.Common;
2+
3+
// FIX: own the reader for the scope with `using`, so it is disposed on every exit path.
4+
static class AdoReaderLeak
5+
{
6+
static int Run(DbCommand cmd)
7+
{
8+
using var reader = cmd.ExecuteReader(); // disposed at scope exit -> clean
9+
var n = 0;
10+
while (reader.Read())
11+
n++;
12+
return n;
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Data.Common;
2+
3+
// A DbDataReader returned by DbCommand.ExecuteReader() is a fresh owned IDisposable the caller
4+
// must dispose -- dropping it leaks the reader and holds the underlying server-side cursor open
5+
// until finalization. The command here is a borrowed parameter (the caller owns it), so the ONLY
6+
// leak is `reader`. This is the single most common real-world ADO.NET resource leak.
7+
static class AdoReaderLeak
8+
{
9+
static int Run(DbCommand cmd)
10+
{
11+
var reader = cmd.ExecuteReader(); // fresh owned DbDataReader -> OWN001 (never disposed)
12+
var n = 0;
13+
while (reader.Read())
14+
n++;
15+
return n; // BUG: reader never disposed
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// OwnLang model of the canonical ADO.NET reader leak (P1a, ADO.NET tranche). A DbDataReader
2+
// from DbCommand.ExecuteReader() is a fresh owned IDisposable the caller must dispose; here it
3+
// is acquired, read, and never released — the generic OWN001 leak. The command is a borrowed
4+
// parameter and the reader does not escape (only a count is returned), so it stays tracked.
5+
// See notes.md for the recognition rule (return type implements System.Data.IDataReader).
6+
module Corpus
7+
resource Reader {
8+
acquire open
9+
release dispose
10+
kind "disposable"
11+
emit_type "DbDataReader"
12+
emit_acquire "{args}.ExecuteReader()"
13+
emit_release "{0}.Dispose()"
14+
}
15+
fn Run(cmd: int) {
16+
let reader = acquire Reader(cmd); // var reader = cmd.ExecuteReader()
17+
// rows read via reader.Read(); no `release reader;` — never disposed (OWN001)
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OWN001
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# ado-executereader-leak
2+
3+
`DbCommand.ExecuteReader()` returns a fresh **owned** `DbDataReader` the caller must
4+
dispose. Dropping it leaks the reader and keeps the server-side cursor/connection busy
5+
until finalization — the single most common real-world ADO.NET resource leak.
6+
7+
- **before.cs** — `var reader = cmd.ExecuteReader();` used and never disposed → `OWN001`.
8+
The command is a borrowed parameter, so the only leak is the reader.
9+
- **after.cs** — `using var reader = …` disposes it on every path → clean.
10+
11+
Recognised by the extractor's `IsOwningFactory` (P1a, ADO.NET tranche): matched by method
12+
name + the resolved return type implementing `System.Data.IDataReader`, so it covers every
13+
provider (`SqlDataReader`, `NpgsqlDataReader`, …), the abstract `DbDataReader`, and the
14+
interface. Sibling members `CreateCommand` (→ `IDbCommand`) and `BeginTransaction`
15+
(→ `IDbTransaction`) are recognised the same way.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,9 +2299,33 @@ static bool IsOwningFactory(ExpressionSyntax? e, SemanticModel model)
22992299
// overloads have no disposable arg and still resolve. (precision over recall.)
23002300
&& !AnyDisposableArgument(i, model))
23012301
return true;
2302+
// ADO.NET owned-returning members — the canonical real-world disposable leak. These are
2303+
// INSTANCE methods on a connection/command/transaction, but the acquire path (op="acquire")
2304+
// does not care static-vs-instance, and the receiver is not an argument so it is never
2305+
// dropped. Provider types vary (SqlCommand / NpgsqlCommand / SqliteCommand / ...), so match
2306+
// by method name + the RESOLVED return type implementing the System.Data contract interface,
2307+
// which covers every provider, the abstract base (DbDataReader/DbCommand/DbTransaction), and
2308+
// the interface itself:
2309+
// * ExecuteReader() -> a DbDataReader the caller must dispose (also frees the cursor)
2310+
// * CreateCommand() -> a DbCommand the caller must dispose
2311+
// * BeginTransaction() -> a DbTransaction the caller must dispose
2312+
// Arguments are non-disposable (CommandBehavior / IsolationLevel enums); the guard keeps any
2313+
// odd overload from dropping a disposable input.
2314+
if (!AnyDisposableArgument(i, model)
2315+
&& ((sym.Name == "ExecuteReader" && ImplementsSystemDataInterface(sym.ReturnType, "IDataReader"))
2316+
|| (sym.Name == "CreateCommand" && ImplementsSystemDataInterface(sym.ReturnType, "IDbCommand"))
2317+
|| (sym.Name == "BeginTransaction" && ImplementsSystemDataInterface(sym.ReturnType, "IDbTransaction"))))
2318+
return true;
23022319
return false;
23032320
}
23042321

2322+
// True if `t` IS, or implements, the named `System.Data` interface (e.g. IDataReader). Covers a
2323+
// provider's concrete type (SqlDataReader), the abstract base (DbDataReader), and the interface.
2324+
static bool ImplementsSystemDataInterface(ITypeSymbol? t, string iface) =>
2325+
t is not null
2326+
&& ((t.Name == iface && IsInNamespace(t as INamedTypeSymbol, "System", "Data"))
2327+
|| t.AllInterfaces.Any(i => i.Name == iface && IsInNamespace(i, "System", "Data")));
2328+
23052329
// True if any argument to the call resolves to a type implementing IDisposable — a disposable
23062330
// the callee might NOT take ownership of, so an enclosing owned-factory claim must be declined
23072331
// rather than silently drop that argument from leak tracking.

0 commit comments

Comments
 (0)