Skip to content

Commit 8c0e93b

Browse files
authored
Fix possible protocol desync with SchemaOnly and auto prepare (#5221)
Fixes #5220
1 parent d8c70e5 commit 8c0e93b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Npgsql/NpgsqlDataReader.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -730,20 +730,21 @@ async Task<bool> NextResultSchemaOnly(bool async, bool isConsuming = false, Canc
730730
default:
731731
throw Connector.UnexpectedMessageReceived(msg.Code);
732732
}
733+
734+
if (_statements.Skip(StatementIndex + 1).All(x => x.IsPrepared))
735+
{
736+
// There are no more queries, we're done. Read to the RFQ.
737+
Expect<ReadyForQueryMessage>(await Connector.ReadMessage(async), Connector);
738+
}
733739
}
734740

735741
// Found a resultset
736742
if (RowDescription != null)
737743
return true;
738744
}
739745

740-
// There are no more queries, we're done. Read to the RFQ.
741-
if (!_statements.All(s => s.IsPrepared))
742-
{
743-
Expect<ReadyForQueryMessage>(await Connector.ReadMessage(async), Connector);
744-
RowDescription = null;
745-
State = ReaderState.Consumed;
746-
}
746+
RowDescription = null;
747+
State = ReaderState.Consumed;
747748

748749
return false;
749750
}

test/Npgsql.Tests/AutoPrepareTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ await conn.ExecuteNonQueryAsync(
514514
Assert.That(await conn.ExecuteScalarAsync("SELECT 3"), Is.EqualTo(3));
515515
}
516516

517-
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4404")]
517+
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4404"), IssueLink("https://github.com/npgsql/npgsql/issues/5220")]
518518
public async Task SchemaOnly()
519519
{
520520
await using var dataSource = CreateDataSource(csb =>
@@ -529,6 +529,9 @@ public async Task SchemaOnly()
529529
{
530530
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SchemaOnly);
531531
}
532+
533+
// Make sure there is no protocol desync due to #5220
534+
await cmd.ExecuteScalarAsync();
532535
}
533536

534537
[Test]

0 commit comments

Comments
 (0)