Skip to content

Commit b673d8a

Browse files
committed
Make statement IDs globally unique
This avoids weird behavior when using a statement with the wrong connection
1 parent de46ba2 commit b673d8a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

tokio-postgres/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use std::collections::HashMap;
7878
use std::fmt;
7979
use std::io;
8080
use std::sync::Arc;
81+
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
8182
use std::sync::mpsc::{self, Sender, Receiver};
8283
use tokio_core::reactor::Handle;
8384

@@ -109,6 +110,8 @@ const TYPEINFO_QUERY: &'static str = "__typeinfo";
109110
const TYPEINFO_ENUM_QUERY: &'static str = "__typeinfo_enum";
110111
const TYPEINFO_COMPOSITE_QUERY: &'static str = "__typeinfo_composite";
111112

113+
static NEXT_STMT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
114+
112115
/// Specifies the TLS support required for a new connection.
113116
pub enum TlsMode {
114117
/// The connection must use TLS.
@@ -163,7 +166,6 @@ struct InnerConnection {
163166
parameters: HashMap<String, String>,
164167
types: HashMap<Oid, Other>,
165168
cancel_data: CancelData,
166-
next_stmt_id: u32,
167169
has_typeinfo_query: bool,
168170
has_typeinfo_enum_query: bool,
169171
has_typeinfo_composite_query: bool,
@@ -283,7 +285,6 @@ impl Connection {
283285
process_id: 0,
284286
secret_key: 0,
285287
},
286-
next_stmt_id: 0,
287288
has_typeinfo_query: false,
288289
has_typeinfo_enum_query: false,
289290
has_typeinfo_composite_query: false,
@@ -969,9 +970,8 @@ impl Connection {
969970
}
970971

971972
/// Creates a new prepared statement.
972-
pub fn prepare(mut self, query: &str) -> BoxFuture<(Statement, Connection), Error> {
973-
let id = self.0.next_stmt_id;
974-
self.0.next_stmt_id += 1;
973+
pub fn prepare(self, query: &str) -> BoxFuture<(Statement, Connection), Error> {
974+
let id = NEXT_STMT_ID.fetch_add(1, Ordering::SeqCst);
975975
let name = format!("s{}", id);
976976
self.raw_prepare(&name, query)
977977
.map(|(params, columns, conn)| {

0 commit comments

Comments
 (0)