From 6fc21a4223ded169ae3dbe87ddb3144c6d3d90b9 Mon Sep 17 00:00:00 2001 From: sakridge Date: Thu, 23 Aug 2018 20:57:13 -0700 Subject: [PATCH] Don't hang in transaction_count (#1052) Situation is there can be that there can be bad entries in the bench-tps CRDT table until they get purged later. Threads however are created for those bad entries and then will hang on trying to get the transaction_count from those bad addresses and never end. --- src/thin_client.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/thin_client.rs b/src/thin_client.rs index 6cf2e24948..4fca472d09 100755 --- a/src/thin_client.rs +++ b/src/thin_client.rs @@ -189,14 +189,14 @@ impl ThinClient { } /// Request the transaction count. If the response packet is dropped by the network, - /// this method will hang. + /// this method will try again 5 times. pub fn transaction_count(&mut self) -> u64 { debug!("transaction_count"); let req = Request::GetTransactionCount; let data = serialize(&req).expect("serialize GetTransactionCount in pub fn transaction_count"); - let mut done = false; - while !done { + let mut tries_left = 5; + while tries_left > 0 { self.requests_socket .send_to(&data, &self.requests_addr) .expect("buffer error in pub fn transaction_count"); @@ -204,9 +204,11 @@ impl ThinClient { if let Ok(resp) = self.recv_response() { debug!("transaction_count recv_response: {:?}", resp); if let Response::TransactionCount { .. } = resp { - done = true; + tries_left = 0; } self.process_response(&resp); + } else { + tries_left -= 1; } } self.transaction_count @@ -544,4 +546,18 @@ mod tests { server.join().unwrap(); remove_dir_all(ledger_path).unwrap(); } + + #[test] + fn test_transaction_count() { + // set a bogus address, see that we don't hang + logger::setup(); + let addr = "0.0.0.0:1234".parse().unwrap(); + let requests_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); + requests_socket + .set_read_timeout(Some(Duration::from_millis(250))) + .unwrap(); + let transactions_socket = UdpSocket::bind("0.0.0.0:0").unwrap(); + let mut client = ThinClient::new(addr, requests_socket, addr, transactions_socket); + assert_eq!(client.transaction_count(), 0); + } }