Skip to content

fix: make connection usable after copy statement execution failure - #1398

Open
sunng87 wants to merge 1 commit into
rust-postgres:masterfrom
sunng87:fix-copy-in-execute-stage-error
Open

sunng87 wants to merge 1 commit into
rust-postgres:masterfrom
sunng87:fix-copy-in-execute-stage-error

Conversation

@sunng87

@sunng87 sunng87 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

When a COPY ... FROM STDIN statement fails at the execute stage (after BindComplete, before CopyInResponse — e.g. the target table does not exist), Client::copy_in returns the server's ErrorResponse as expected, but the connection is left in a corrupted state. Every subsequent statement on the same Client fails with Closed or UnexpectedMessage.

Repro:

   let (client, conn) = tokio_postgres::connect(&url, NoTls).await?;
   tokio::spawn(conn);

   // fails with SQLSTATE 42P01, as expected
   client.copy_in::<_, Bytes>("COPY missing_tbl FROM STDIN").await.unwrap_err();

   // before this fix: Err(Closed) / Err(UnexpectedMessage)
   client.query("SELECT 1", &[]).await?;

CopyInReceiver now tracks whether the server ever entered copy-in mode: copy_in() sends a new CopyInMessage::Starte marker after receiving CopyInResponse. On drop-without-finish, the trailing CopyFail + Sync is only emitted if the copy actually started; otherwise the stream simply ends, since the exchange was already terminated by the server's error.

@sunng87 sunng87 changed the title fix: make connection usable after copy failure fix: make connection usable after copy statement execution failure Oct 26, 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.

1 participant