[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.
This commit is contained in:
Akihito Nakano 2021-01-14 00:04:18 +00:00
parent 28238d97b1
commit e5b1a37110

View File

@ -101,14 +101,15 @@ impl<E: EthSpec> LocalNetwork<E> {
beacon_config.network.enr_tcp_port = Some(BOOTNODE_PORT + count); 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( let beacon_node = LocalBeaconNode::production(
self.context.service_context(format!("node_{}", index)), self.context.service_context(format!("node_{}", index)),
beacon_config, beacon_config,
) )
.await?; .await?;
self_1.beacon_nodes.write().push(beacon_node); write_lock.push(beacon_node);
Ok(()) Ok(())
} }