Skip to content

Commit

Permalink
Fix possible protocol desync with SchemaOnly and auto prepare (#5221)
Browse files Browse the repository at this point in the history
Fixes #5220

(cherry picked from commit 8c0e93b)
  • Loading branch information
vonzshik committed Aug 25, 2023
1 parent 29d9395 commit 8060929
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/Npgsql/NpgsqlDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,20 +728,21 @@ async Task<bool> NextResultSchemaOnly(bool async, bool isConsuming = false, Canc
default:
throw Connector.UnexpectedMessageReceived(msg.Code);
}

if (_statements.Skip(StatementIndex + 1).All(x => x.IsPrepared))
{
// There are no more queries, we're done. Read to the RFQ.
Expect<ReadyForQueryMessage>(await Connector.ReadMessage(async), Connector);
}
}

// Found a resultset
if (RowDescription != null)
return true;
}

// There are no more queries, we're done. Read to the RFQ.
if (!_statements.All(s => s.IsPrepared))
{
Expect<ReadyForQueryMessage>(await Connector.ReadMessage(async), Connector);
RowDescription = null;
State = ReaderState.Consumed;
}
RowDescription = null;
State = ReaderState.Consumed;

return false;
}
Expand Down
5 changes: 4 additions & 1 deletion test/Npgsql.Tests/AutoPrepareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public async Task Batch_statement_execution_error_cleanup()
Assert.That(await conn.ExecuteScalarAsync("SELECT 3"), Is.EqualTo(3));
}

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4404")]
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4404"), IssueLink("https://github.com/npgsql/npgsql/issues/5220")]
public async Task SchemaOnly()
{
var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
Expand All @@ -559,6 +559,9 @@ public async Task SchemaOnly()
{
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SchemaOnly);
}

// Make sure there is no protocol desync due to #5220
await cmd.ExecuteScalarAsync();
}

[Test]
Expand Down

0 comments on commit 8060929

Please sign in to comment.