Skip to content

Commit 798b497

Browse files
committed
docs(P-034): fix double-dispose example to gate DisposeCore(), not just the throw
Codex review on #192: if only the throw were compiled out for production (as the Non-goals bullet suggested — #if DEBUG around the throw, or a [Conditional] helper), execution fell through the guard branch and called DisposeCore() again on a second Dispose() — a real double-release bug in exactly the builds this proposal exists to keep safe. Fixed the example so the already-disposed branch always returns; only the diagnostic throw is DEBUG-gated via a [Conditional("DEBUG")] helper, and the idempotent short-circuit is unconditional.
1 parent d1a6109 commit 798b497

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

‎docs/proposals/P-034-runtime-lifetime-guard.md‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ Two small, independent, opt-in runtime helpers (a "diagnostic mode", not a
7373
shipped allocator):
7474

7575
**1. `LifetimeGuard` base / wrapper — loud instead of silent.**
76-
A `DEBUG`/`TEST`-only `IDisposable` base that turns silent misuse — a
77-
double-dispose, or an access after `Dispose()` — into an immediate
78-
`ObjectDisposedException`/`InvalidOperationException` instead of corrupting
79-
state quietly, regardless of whether the root cause was an unmodeled
80-
ownership hand-off (P-005 D5) or a cross-thread race:
76+
A `DEBUG`/`TEST`-only *diagnostic throw* layered over an **always-idempotent**
77+
double-dispose guard: the second `Dispose()` call must short-circuit
78+
unconditionally (matching the standard, production-safe C# Dispose pattern),
79+
and only the loud signal — an `ObjectDisposedException` naming the offending
80+
type — is compiled out in release builds:
8181

8282
```csharp
8383
public abstract class LifetimeGuard : IDisposable
@@ -86,15 +86,30 @@ public abstract class LifetimeGuard : IDisposable
8686
public void Dispose()
8787
{
8888
if (Interlocked.Exchange(ref _disposed, 1) != 0)
89-
throw new ObjectDisposedException(GetType().Name, "double Dispose");
89+
{
90+
ThrowIfDoubleDispose(); // DEBUG-only; a no-op in release, but the branch always returns either way
91+
return; // unconditional — DisposeCore() must never run twice, throw or no throw
92+
}
9093
DisposeCore();
9194
GC.SuppressFinalize(this);
9295
}
96+
[Conditional("DEBUG")]
97+
private void ThrowIfDoubleDispose() =>
98+
throw new ObjectDisposedException(GetType().Name, "double Dispose");
9399
protected void ThrowIfDisposed() { if (Volatile.Read(ref _disposed) != 0) throw new ObjectDisposedException(GetType().Name); }
94100
protected abstract void DisposeCore();
95101
}
96102
```
97103

104+
Gating only the `throw` (e.g. wrapping just that line in `#if DEBUG`) and
105+
leaving the surrounding `if` block to fall through is a trap: in release
106+
builds `DisposeCore()` would then run a second time on a second `Dispose()`
107+
call — a real double-release, in exactly the build where this guard is
108+
supposed to be safe-by-default. The `return` above is unconditional for
109+
that reason; `[ConditionalAttribute]` only strips the call to
110+
`ThrowIfDoubleDispose()` itself (valid here — it decorates a `void` method,
111+
per the attribute's own constraint), never the guard's control flow.
112+
98113
This does not replace `OWN002`/`OWN003` — those catch the mistake at compile
99114
time, for free, when the pattern is intraprocedural. `LifetimeGuard` catches
100115
what's left: it surfaces misuse *at the point it happens*, even when the
@@ -132,14 +147,13 @@ debug assertion than an auditor.
132147

133148
- Not a general-purpose allocator shim; nothing here wraps `malloc`/GC
134149
allocation.
135-
- Not a production-safe pattern as-is: throwing from `Dispose()` is a real
136-
behavior change (already-suppressed `Dispose` exceptions in `finally`/`using`
137-
chains can mask the original exception) — `LifetimeGuard` must ship
138-
`DEBUG`/`TEST`-gated: wrap the throw in `#if DEBUG` / `#if TEST`, or route it
139-
through a small helper method decorated `[Conditional("DEBUG")]` (the
140-
attribute only applies to methods, not to a bare `throw` statement), or gate
141-
it behind a config flag — never silently opt production code into new
142-
exceptions.
150+
- Not a production-safe pattern if only the throw is gated and the
151+
already-disposed branch is left to fall through — see the worked example
152+
above; the short-circuit (`return`) must be unconditional, only the
153+
diagnostic `throw` is `DEBUG`-gated (via `[Conditional("DEBUG")]`, since the
154+
attribute only applies to methods, never to a bare `throw` statement).
155+
`LifetimeGuard` must never silently opt production code into new exceptions,
156+
but it also must never silently double-run `DisposeCore()` in production.
143157
- Not a replacement for `OWN002`/`OWN003` (compile-time, zero runtime cost,
144158
works before the code ever ships) or for OwnAudit phase 5 (ground-truth heap
145159
retention) — it fills the gap between them: cases neither can see, at the

0 commit comments

Comments
 (0)