Skip to content

fix(tokio-postgres,copy): fix copy_in double-Sync protocol crash - #1337

Open
NikolayS wants to merge 3 commits into
rust-postgres:masterfrom
NikolayS:fix/copy-in-double-sync-v2
Open

NikolayS wants to merge 3 commits into
rust-postgres:masterfrom
NikolayS:fix/copy-in-double-sync-v2

Conversation

@NikolayS

Copy link
Copy Markdown

(another problem I encountered with while working on https://github.com/NikolayS/rpg)

Bug

copy_in() calls query::encode() which sends Bind+Execute+Sync. Then CopyInReceiver sends another Sync with CopyDone/CopyFail. Two Syncs produce two ReadyForQuery messages. The second arrives when the connection driver's response queue is empty, crashing with "unexpected message from server".

We encountered this in rpg when running PostgreSQL's copy2 regression test — multi-statement COPY sequences would crash the connection.

Why PostgreSQL masks this in the success path: PG explicitly ignores Sync during COPY-IN mode (protocol spec §55.2.6). So for successful COPYs the extra Sync is silently dropped. But if the server rejects the COPY before entering COPY mode (e.g. table dropped between prepare and execute), the Sync is processed normally → double ReadyForQuery → crash. Non-PG wire-compatible servers may not ignore Sync during COPY mode at all, making the success path crash too.

Fix

Replace Sync with Flush ('H') in the initial Bind+Execute message:

  • No Sync → no double ReadyForQuery → no crash
  • Flush → server delivers buffered output (BindComplete, CopyInResponse, or ErrorResponse) without producing ReadyForQuery → no hang when COPY mode is never entered

Red/green TDD

Commit history demonstrates the fix:

  1. RED (5e003fe): adds copy_in_initial_message_has_no_sync test — fails because encode_copy_in delegates to query::encode (includes Sync, no Flush)
  2. GREEN (c1055b4): adds encode_no_sync (Bind+Execute+Flush), wires encode_copy_in to use it — test passes

claude added 3 commits April 10, 2026 00:38
Add copy_in_initial_message_has_no_sync test that asserts copy_in's
initial Bind+Execute message contains no Sync (and includes a Flush).

This test FAILS because encode_copy_in currently delegates to
query::encode which appends a Sync.  CopyInReceiver also sends Sync
with CopyDone/CopyFail, so the server receives two Syncs and may
reply with two ReadyForQuery messages, crashing the connection.

https://claude.ai/code/session_017Lx8faWtxGJDiU2rbqGeHm
Add encode_no_sync() that sends Bind+Execute+Flush instead of
Bind+Execute+Sync.  Wire encode_copy_in() to use it.

This fixes the double-Sync crash: copy_in no longer sends Sync in
the initial message, so CopyInReceiver's Sync with CopyDone/CopyFail
is the only one — exactly one ReadyForQuery per COPY cycle.

The Flush message forces the server to deliver buffered output without
producing ReadyForQuery, which prevents a hang when the server rejects
the COPY before entering COPY mode (error path).

https://claude.ai/code/session_017Lx8faWtxGJDiU2rbqGeHm
@NikolayS NikolayS changed the title fix(copy): fix copy_in double-Sync protocol crash fix(tokio-postgres,copy): fix copy_in double-Sync protocol crash Apr 10, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants