Skip to content

Commit 5543374

Browse files
committed
XXX: Hack statement ID to be random instead of sequential
Use random statement IDs instead of sequence to avoid conflicts (almost 100% of the time). This obviously is not the solution to the problem. It is only the proof of concept to verify that this is the root cause of the problem.
1 parent dfd9cf5 commit 5543374

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

sqlx-core/src/postgres/connection/establish.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::postgres::message::{
88
Authentication, BackendKeyData, MessageFormat, Password, ReadyForQuery, Startup,
99
};
1010
use crate::postgres::{PgConnectOptions, PgConnection};
11+
use rand::thread_rng;
12+
use rand::Rng;
1113

1214
// https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.3
1315
// https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.11
@@ -142,7 +144,7 @@ impl PgConnection {
142144
transaction_status,
143145
transaction_depth: 0,
144146
pending_ready_for_query_count: 0,
145-
next_statement_id: 1,
147+
next_statement_id: thread_rng().gen(),
146148
cache_statement: StatementCache::new(options.statement_cache_capacity),
147149
cache_type_oid: HashMap::new(),
148150
cache_type_info: HashMap::new(),

sqlx-core/src/postgres/connection/executor.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use futures_core::future::BoxFuture;
1717
use futures_core::stream::BoxStream;
1818
use futures_core::Stream;
1919
use futures_util::{pin_mut, TryStreamExt};
20+
use rand::thread_rng;
21+
use rand::Rng;
2022
use std::{borrow::Cow, sync::Arc};
2123

2224
async fn prepare(
@@ -26,7 +28,13 @@ async fn prepare(
2628
metadata: Option<Arc<PgStatementMetadata>>,
2729
) -> Result<(u32, Arc<PgStatementMetadata>), Error> {
2830
let id = conn.next_statement_id;
29-
conn.next_statement_id = conn.next_statement_id.wrapping_add(1);
31+
32+
// XXX(mq): Use random statement IDs instead of sequence to avoid conflicts (almost
33+
// 100% of the time). This obviously is not the solution to the problem.
34+
// It is only the proof of concept to verify that this is the root cause
35+
// of the problem.
36+
//conn.next_statement_id = conn.next_statement_id.wrapping_add(1);
37+
conn.next_statement_id = thread_rng().gen();
3038

3139
// build a list of type OIDs to send to the database in the PARSE command
3240
// we have not yet started the query sequence, so we are *safe* to cleanly make

0 commit comments

Comments
 (0)