Skip to content

Commit ad724dd

Browse files
authored
Fixed nre while disposing NpgsqlRawCopyStream (#3618)
Fixes #3600
1 parent 8879761 commit ad724dd

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/Npgsql/NpgsqlRawCopyStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ void Cleanup()
442442
{
443443
Log.Debug("COPY operation ended", _connector.Id);
444444
_connector.CurrentCopyOperation = null;
445-
_connector.Connection!.EndBindingScope(ConnectorBindingScope.Copy);
445+
_connector.Connection?.EndBindingScope(ConnectorBindingScope.Copy);
446446
_connector = null;
447447
_readBuf = null;
448448
_writeBuf = null;

test/Npgsql.Tests/BugTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Data;
4-
using System.Linq;
53
using System.Text;
64
using System.Threading;
75
using System.Threading.Tasks;
86
using NpgsqlTypes;
97
using NUnit.Framework;
108
using System.Transactions;
9+
using Npgsql.Tests.Support;
10+
11+
using static Npgsql.Tests.TestUtil;
1112

1213
namespace Npgsql.Tests
1314
{
@@ -116,6 +117,27 @@ public void Bug1645()
116117
Assert.That(conn.ExecuteScalar("SELECT COUNT(*) FROM data"), Is.Zero);
117118
}
118119

120+
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/3600")]
121+
public async Task Bug3600()
122+
{
123+
var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
124+
{
125+
CommandTimeout = 1,
126+
};
127+
await using var postmasterMock = PgPostmasterMock.Start(csb.ConnectionString);
128+
using var _ = CreateTempPool(postmasterMock.ConnectionString, out var connectionString);
129+
await using var conn = await OpenConnectionAsync(connectionString);
130+
var serverMock = await postmasterMock.WaitForServerConnection();
131+
await serverMock
132+
.WriteCopyInResponse()
133+
.FlushAsync();
134+
var ex = Assert.ThrowsAsync<NpgsqlException>(async () =>
135+
{
136+
await using var importer = await conn.BeginTextImportAsync($"COPY SomeTable (field_text, field_int4) FROM STDIN");
137+
});
138+
Assert.That(ex!.InnerException, Is.TypeOf<TimeoutException>());
139+
}
140+
119141
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/1497")]
120142
public void Bug1497()
121143
{

test/Npgsql.Tests/Support/PgServerMock.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,17 @@ internal PgServerMock WriteBackendKeyData(int processId, int secret)
265265
internal PgServerMock WriteCancellationResponse()
266266
=> WriteErrorResponse(PostgresErrorCodes.QueryCanceled, "Cancellation", "Query cancelled");
267267

268+
internal PgServerMock WriteCopyInResponse()
269+
{
270+
CheckDisposed();
271+
_writeBuffer.WriteByte((byte)BackendMessageCode.CopyInResponse);
272+
_writeBuffer.WriteInt32(5);
273+
_writeBuffer.WriteByte(0);
274+
_writeBuffer.WriteInt16(1);
275+
_writeBuffer.WriteInt16(0);
276+
return this;
277+
}
278+
268279
internal PgServerMock WriteErrorResponse(string code)
269280
=> WriteErrorResponse(code, "ERROR", "MOCK ERROR MESSAGE");
270281

0 commit comments

Comments
 (0)