From e5b1a37110b72db03d5eea98a7c317b491157941 Mon Sep 17 00:00:00 2001 From: Akihito Nakano Date: Thu, 14 Jan 2021 00:04:18 +0000 Subject: [PATCH] [simulator] Fix race condition when creating LocalBeaconNode (#2137) ## Issue Addressed We have a race condition when counting the number of beacon nodes. The user could end up seeing a duplicated service name (node_N). ## Proposed Changes I have updated to acquire write lock before counting the number of beacon nodes. --- testing/simulator/src/local_network.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/testing/simulator/src/local_network.rs b/testing/simulator/src/local_network.rs index d4f1aa6ca..b8eca05b5 100644 --- a/testing/simulator/src/local_network.rs +++ b/testing/simulator/src/local_network.rs @@ -101,14 +101,15 @@ impl LocalNetwork { beacon_config.network.enr_tcp_port = Some(BOOTNODE_PORT + count); } - let index = self.beacon_nodes.read().len(); + let mut write_lock = self_1.beacon_nodes.write(); + let index = write_lock.len(); let beacon_node = LocalBeaconNode::production( self.context.service_context(format!("node_{}", index)), beacon_config, ) .await?; - self_1.beacon_nodes.write().push(beacon_node); + write_lock.push(beacon_node); Ok(()) }