renproject / aw

A flexible P2P networking library for upgradable distributed systems.
MIT License
38 stars 18 forks source link

Fix a potential panic in dht. #87

Closed tok-kkk closed 3 years ago

tok-kkk commented 3 years ago

The panic happens when calling both RandomPeers and DeletePeer in parallel. The RandomPeers function didn't hold the mutex throughout the function and would encounter an index-out-of-range if the address had been deleted. A test has been added to reproduce the panic.

The fix changes the RandomPeers function to hold the mutex until returning. The performance still looks acceptable in my opinion (90 microseconds with 50-100 peers in the dht)

rahulghangas commented 3 years ago

Good catch