Skip to content

Commit d645252

Browse files
authored
Fix deadlocks by removing std MutexGuards held across awaits. (yugabyte#4)
1 parent c9faa3d commit d645252

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

tokio-postgres/src/connect.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::task::Poll;
1717
use std::time::Instant;
1818
use std::{cmp, io};
1919
use tokio::net;
20+
use tokio::sync::Mutex as TokioMutex;
2021

2122
lazy_static! {
2223
static ref CONNECTION_COUNT_MAP: Mutex<HashMap<Host, i64>> = {
@@ -31,9 +32,9 @@ lazy_static! {
3132
}
3233
Mutex::new(m)
3334
};
34-
static ref LAST_TIME_META_DATA_FETCHED: Mutex<Instant> = {
35+
static ref LAST_TIME_META_DATA_FETCHED: TokioMutex<Instant> = {
3536
let m = Instant::now();
36-
Mutex::new(m)
37+
TokioMutex::new(m)
3738
};
3839
static ref HOST_INFO: Mutex<Vec<Host>> = {
3940
let m = Vec::new();
@@ -350,7 +351,7 @@ fn get_least_loaded_server(config: &Config) -> Option<Host> {
350351
}
351352

352353
async fn check_and_refresh(config: &Config) -> bool {
353-
let mut refresh_time = LAST_TIME_META_DATA_FETCHED.lock().unwrap();
354+
let mut refresh_time = LAST_TIME_META_DATA_FETCHED.lock().await;
354355
let host_list = HOST_INFO.lock().unwrap().clone();
355356
if host_list.len() == 0 {
356357
info!("Connecting to the server for the first time");
@@ -448,17 +449,18 @@ async fn refresh(client: Client, config: &Config) {
448449
control_conn_host = socket_config.unwrap().hostname.unwrap();
449450
}
450451

452+
info!("Executing query: `select * from yb_servers()` to fetch list of servers");
453+
let rows = client
454+
.query("select * from yb_servers()", &[])
455+
.await
456+
.unwrap();
457+
451458
let mut host_list = HOST_INFO.lock().unwrap();
452459
let mut failed_host_list = FAILED_HOSTS.lock().unwrap();
453460
let mut placement_info_map = PLACEMENT_INFO_MAP.lock().unwrap();
454461
let mut public_host_map = PUBLIC_HOST_MAP.lock().unwrap();
455462
let mut host_to_port_map = HOST_TO_PORT_MAP.lock().unwrap();
456-
info!("Executing query: `select * from yb_servers()` to fetch list of servers");
457-
for row in client
458-
.query("select * from yb_servers()", &[])
459-
.await
460-
.unwrap()
461-
{
463+
for row in rows {
462464
let host_string: String = row.get("host");
463465
let host = Host::Tcp(host_string.to_string());
464466
info!("Received entry for host {:?}", host);

0 commit comments

Comments
 (0)