Fix some more state cleanup cases · npgsql/npgsql@467aca3 · GitHub
Skip to content

Commit 467aca3

Browse files
committed
Fix some more state cleanup cases
1 parent f96d6ad commit 467aca3

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎src/Npgsql/Internal/Converters/StreamConverter.cs‎

Lines changed: 1 addition & 1 deletion

‎src/Npgsql/Internal/PgConverter.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ public Size BindAsObject(in BindContext context, object? value, ref object? writ
203203
// writeState pointing at a half-disposed object — they'd dispose it again.
204204
(var current, writeState) = (writeState, null);
205205
(current as IDisposable)?.Dispose();
206-
if (!ReferenceEquals(current, originalWriteState))
206+
// current==null with original!=null means an inner safety net (transparent wrapper case)
207+
// already disposed the original. Disposing again would double-dispose. The non-null branch
208+
// handles the genuine lifecycle-violation swap where current and original differ.
209+
if (current is not null && !ReferenceEquals(current, originalWriteState))
207210
(originalWriteState as IDisposable)?.Dispose();
208211
throw;
209212
}
@@ -377,7 +380,10 @@ public Size Bind(in BindContext context,
377380
// writeState pointing at a half-disposed object — they'd dispose it again.
378381
(var current, writeState) = (writeState, null);
379382
(current as IDisposable)?.Dispose();
380-
if (!ReferenceEquals(current, originalWriteState))
383+
// current==null with original!=null means an inner safety net (transparent wrapper case)
384+
// already disposed the original. Disposing again would double-dispose. The non-null branch
385+
// handles the genuine lifecycle-violation swap where current and original differ.
386+
if (current is not null && !ReferenceEquals(current, originalWriteState))
381387
(originalWriteState as IDisposable)?.Dispose();
382388
throw;
383389
}

‎src/Npgsql/NpgsqlParameter.cs‎

Lines changed: 13 additions & 0 deletions

0 commit comments

Comments
 (0)