Skip to content

Commit 72a312d

Browse files
authored
More correct pg wire protocol error handling (#8348)
1 parent 25b8b9b commit 72a312d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
- Fixed an issue where credentials from `firebase login` would not be correctly provided to the Data Connect emulator.
1212
- Fixed misleading comments in `firebase init dataconnect` `connector.yaml` template.
1313
- Improved Data Connect SQL permissions to better handle tables owned by IAM roles. (#8339)
14+
- Fixed an issue where the Data Connect emulator would crash after some SQL errors.

src/emulator/dataconnect/pgliteServer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export class PostgresServer {
164164
// TODO: Remove this code once https://github.com/electric-sql/pglite/pull/294 is released in PGLite
165165
export class PGliteExtendedQueryPatch {
166166
isExtendedQuery = false;
167+
eqpErrored = false;
167168

168169
constructor(public connection: PostgresConnection) {}
169170

@@ -184,16 +185,21 @@ export class PGliteExtendedQueryPatch {
184185
// 'Sync' indicates the end of an extended query
185186
if (message[0] === FrontendMessageCode.Sync) {
186187
this.isExtendedQuery = false;
188+
this.eqpErrored = false;
187189

188190
// Manually inject 'ReadyForQuery' message at the end
189191
return this.connection.createReadyForQuery();
190192
}
191193

192194
// A PGlite response can contain multiple messages
193195
for await (const message of getMessages(response)) {
194-
// If a prepared statement leads to an error message, we need to end the pipeline.
195-
if (message[0] === BackendMessageCode.ErrorMessage) {
196-
this.isExtendedQuery = false;
196+
// After an ErrorMessage in extended query protocol, we should throw away messages until the next Sync
197+
// (per https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY:~:text=When%20an%20error,for%20each%20Sync.))
198+
if (this.eqpErrored) {
199+
continue;
200+
}
201+
if (this.isExtendedQuery && message[0] === BackendMessageCode.ErrorMessage) {
202+
this.eqpErrored = true;
197203
}
198204
// Filter out incorrect `ReadyForQuery` messages during the extended query protocol
199205
if (this.isExtendedQuery && message[0] === BackendMessageCode.ReadyForQuery) {

0 commit comments

Comments
 (0)