From c47630366c074d9baddbe97e379126ed6cdf9e7e Mon Sep 17 00:00:00 2001 From: draoi Date: Tue, 2 Apr 2024 13:44:45 +0200 Subject: [PATCH] refinery: acquire exclusive lock on greylist before refining Modifications to the greylist (appending or deleting from) are dangerous while the refinery is ongoing, since it can lead to panics in the case the refinery fails (because we delete from the greylist by index when that happens). We mitigate this issue by acquiring an exclusive lock on the greylist before proceeding with the refinery, and dropping the lock once the refinery process has finished and greylist modifications are no longer dangerous. --- src/net/session/refine_session.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/net/session/refine_session.rs b/src/net/session/refine_session.rs index 10351f364..c2a52b3e7 100644 --- a/src/net/session/refine_session.rs +++ b/src/net/session/refine_session.rs @@ -249,8 +249,12 @@ impl GreylistRefinery { continue } + // Freeze the greylist in this state. Necessary since the greylist + // can be modified by `hosts::move_host()`. + let mut greylist = hosts.container.hostlists[HostColor::Grey as usize].write().await; + if !self.session().handshake_node(url.clone(), self.p2p().clone()).await { - hosts.container.remove(HostColor::Grey, url, position).await; + greylist.remove(position); debug!( target: "net::refinery", @@ -262,9 +266,13 @@ impl GreylistRefinery { // modification is now complete. hosts.unregister(url).await; + drop(greylist); + continue } + drop(greylist); + debug!( target: "net::refinery", "Peer {} is responsive. Adding to whitelist", url,