Skip to content

Commit c1055b4

Browse files
committed
fix(copy): replace Sync with Flush in copy_in encoding (GREEN)
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
1 parent 5e003fe commit c1055b4

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎tokio-postgres/src/copy_in.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub(crate) fn encode_copy_in(
197197
client: &InnerClient,
198198
statement: &Statement,
199199
) -> Result<bytes::Bytes, Error> {
200-
query::encode(client, statement, slice_iter(&[]))
200+
query::encode_no_sync(client, statement, slice_iter(&[]))
201201
}
202202

203203
pub async fn copy_in<T>(client: &InnerClient, statement: Statement) -> Result<CopyInSink<T>, Error>

‎tokio-postgres/src/query.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,34 @@ where
267267
})
268268
}
269269

270+
/// Like [`encode`] but replaces the trailing Sync with a Flush message.
271+
///
272+
/// Used by `copy_in` where the Sync must be deferred: CopyInReceiver sends
273+
/// the sole Sync together with CopyDone (success) or CopyFail (abort).
274+
/// Sending two Syncs would produce two ReadyForQuery messages from the
275+
/// server, crashing the connection driver.
276+
///
277+
/// The Flush forces the server to deliver buffered output (BindComplete,
278+
/// CopyInResponse, or ErrorResponse) without producing a ReadyForQuery,
279+
/// preventing a hang when the server never enters COPY mode.
280+
pub fn encode_no_sync<P, I>(
281+
client: &InnerClient,
282+
statement: &Statement,
283+
params: I,
284+
) -> Result<Bytes, Error>
285+
where
286+
P: BorrowToSql,
287+
I: IntoIterator<Item = P>,
288+
I::IntoIter: ExactSizeIterator,
289+
{
290+
client.with_buf(|buf| {
291+
encode_bind(statement, params, "", buf)?;
292+
frontend::execute("", 0, buf).map_err(Error::encode)?;
293+
frontend::flush(buf);
294+
Ok(buf.split().freeze())
295+
})
296+
}
297+
270298
pub fn encode_bind<P, I>(
271299
statement: &Statement,
272300
params: I,

0 commit comments

Comments
 (0)