Skip to content

Commit 5e003fe

Browse files
committed
test(copy): add failing test for copy_in double-Sync bug (RED)
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
1 parent 2adc59b commit 5e003fe

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎tokio-postgres/src/client.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ pub struct InnerClient {
9393
}
9494

9595
impl InnerClient {
96+
#[cfg(test)]
97+
pub(crate) fn new_for_test() -> InnerClient {
98+
let (tx, _rx) = mpsc::unbounded();
99+
InnerClient {
100+
sender: tx,
101+
cached_typeinfo: Default::default(),
102+
buffer: Mutex::new(BytesMut::new()),
103+
}
104+
}
105+
96106
pub fn send(&self, messages: RequestMessages) -> Result<Responses, Error> {
97107
let (sender, receiver) = mpsc::channel(1);
98108
let request = Request { messages, sender };

‎tokio-postgres/src/copy_in.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,25 @@ where
188188
}
189189
}
190190

191+
/// Encode the initial Bind+Execute for a COPY IN statement.
192+
///
193+
/// Must not include a Sync — CopyInReceiver sends the sole Sync with
194+
/// CopyDone/CopyFail. A trailing Flush forces the server to deliver
195+
/// its response even if COPY mode is never entered (error path).
196+
pub(crate) fn encode_copy_in(
197+
client: &InnerClient,
198+
statement: &Statement,
199+
) -> Result<bytes::Bytes, Error> {
200+
query::encode(client, statement, slice_iter(&[]))
201+
}
202+
191203
pub async fn copy_in<T>(client: &InnerClient, statement: Statement) -> Result<CopyInSink<T>, Error>
192204
where
193205
T: Buf + 'static + Send,
194206
{
195207
debug!("executing copy in statement {}", statement.name());
196208

197-
let buf = query::encode(client, &statement, slice_iter(&[]))?;
209+
let buf = encode_copy_in(client, &statement)?;
198210

199211
let (mut sender, receiver) = mpsc::channel(1);
200212
let receiver = CopyInReceiver::new(receiver);
@@ -223,3 +235,34 @@ where
223235
_p2: PhantomData,
224236
})
225237
}
238+
239+
#[cfg(test)]
240+
mod tests {
241+
use super::encode_copy_in;
242+
use crate::client::InnerClient;
243+
use crate::Statement;
244+
245+
/// Wire bytes for Sync ('S', length 4) and Flush ('H', length 4).
246+
const SYNC_BYTES: [u8; 5] = [b'S', 0, 0, 0, 4];
247+
const FLUSH_BYTES: [u8; 5] = [b'H', 0, 0, 0, 4];
248+
249+
/// The initial Bind+Execute that `copy_in` sends must NOT contain a
250+
/// Sync message. CopyInReceiver sends the sole Sync with CopyDone or
251+
/// CopyFail; a second Sync here would produce two ReadyForQuery
252+
/// responses, crashing the connection driver.
253+
///
254+
/// It SHOULD contain a Flush so the server delivers its response even
255+
/// when COPY mode is never entered (error path).
256+
#[test]
257+
fn copy_in_initial_message_has_no_sync() {
258+
let client = InnerClient::new_for_test();
259+
let statement = Statement::unnamed(vec![], vec![]);
260+
let buf = encode_copy_in(&client, &statement).unwrap();
261+
262+
let syncs = buf.windows(5).filter(|w| *w == SYNC_BYTES).count();
263+
assert_eq!(syncs, 0, "must not contain Sync");
264+
265+
let flushes = buf.windows(5).filter(|w| *w == FLUSH_BYTES).count();
266+
assert_eq!(flushes, 1, "must contain Flush");
267+
}
268+
}

0 commit comments

Comments
 (0)