|
| 1 | +# `TcpListener.Stop()` is a release — Stop() is the cleanup, not a half-measure |
| 2 | + |
| 3 | +A precision fix from a **Codex review** (PR #61). While triaging the ShareX re-mine I |
| 4 | +nearly locked an undisposed-`TcpListener` finding as a corpus regression — Codex caught |
| 5 | +that it is a **false positive**, and it was right. |
| 6 | + |
| 7 | +## The false positive |
| 8 | + |
| 9 | +```csharp |
| 10 | +public static int GetRandomUnusedPort() // ShareX WebHelpers.cs:191 |
| 11 | +{ |
| 12 | + TcpListener listener = new TcpListener(IPAddress.Loopback, 0); |
| 13 | + try |
| 14 | + { |
| 15 | + listener.Start(); |
| 16 | + return ((IPEndPoint)listener.LocalEndpoint).Port; |
| 17 | + } |
| 18 | + finally |
| 19 | + { |
| 20 | + listener.Stop(); |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +The flow detector flagged `listener` OWN001: it is an `IDisposable` (`TcpListener` |
| 26 | +implements `IDisposable` since .NET Core 3.0), and `Stop()` is not in the release set |
| 27 | +(`Dispose`/`Close`/`DisposeAsync`). But **`TcpListener.Dispose()` just delegates to |
| 28 | +`Stop()`** — `Stop()` disposes the listen socket and clears it. So after `Stop()` there is |
| 29 | +no held resource: an undisposed-but-stopped listener is **not a leak**, and flagging it |
| 30 | +dents the 0-FP precision claim (CA2000 / CodeQL flag it conventionally, but it is not a |
| 31 | +real resource leak). |
| 32 | + |
| 33 | +## The fix — Stop() is a release, for `TcpListener` |
| 34 | + |
| 35 | +`EmitFlowExpr` now models `tcpListener.Stop()` as a `release` of the local, alongside the |
| 36 | +`Dispose()`/`Close()`/pool-`Return`/`Show()` releases: |
| 37 | + |
| 38 | +```csharp |
| 39 | +if (expr is InvocationExpressionSyntax tlinv |
| 40 | + && tlinv.Expression is MemberAccessExpressionSyntax |
| 41 | + { Name.Identifier.Text: "Stop", Expression: IdentifierNameSyntax tlid } |
| 42 | + && tracked.Contains(tlid.Identifier.Text) |
| 43 | + && model.GetSymbolInfo(tlinv).Symbol is IMethodSymbol { ContainingType: { Name: "TcpListener" } tct } |
| 44 | + && IsInNamespace(tct, "System", "Net", "Sockets")) |
| 45 | +{ |
| 46 | + nodes.Add(new { op = "release", var = tlid.Identifier.Text, line = LineOf(tlinv) }); |
| 47 | + return; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +It is **`TcpListener`-specific**, resolved on the method's containing type via the |
| 52 | +SemanticModel — `Stop()` on a `Timer` / `Process` / `Stopwatch` / etc. does **not** dispose |
| 53 | +and is untouched (stays a tracked use). And it is path-sensitive (a release at the call |
| 54 | +site), like the WinForms `Show()` model: a listener never `Stop()`'d (nor disposed) still |
| 55 | +leaks on the path that misses it. |
| 56 | + |
| 57 | +## Pinned in CI |
| 58 | + |
| 59 | +Two `FlowLocalsSample.cs` cases in the `wpf-extractor` `--flow-locals` step: |
| 60 | + |
| 61 | +- `TcpListenerStopped` — `new TcpListener(...); Start(); Stop();` → **silent** (the FP this |
| 62 | + removes); |
| 63 | +- `TcpListenerNeverStopped` — `new TcpListener(...); Start();` (no `Stop`) → **OWN001**, the |
| 64 | + control proving the release is `Stop()`-specific, not a blanket `TcpListener` exemption. |
| 65 | + |
| 66 | +## The sibling that is *not* fixed (and why) |
| 67 | + |
| 68 | +The same triage had a `StreamReader` finding (a reader over a `using`-scoped `MemoryStream`, |
| 69 | +the reader not disposed). That is also a non-leak *in that specific shape* — the stream is |
| 70 | +disposed separately and the reader holds no unmanaged resource of its own. But unlike |
| 71 | +`TcpListener`, the extractor's general behaviour there is **correct**: a `StreamReader` over |
| 72 | +a stream that is *not* otherwise disposed genuinely leaks the stream (the reader owns it). |
| 73 | +Suppressing it would need stream-ownership reasoning and would cost real recall, so it is |
| 74 | +left as-is — the narrow `using`-scoped-stream variant is an accepted, rare residual, not a |
| 75 | +systematic FP class like `TcpListener.Stop()`. |
0 commit comments