Prevent deadlock

This commit is contained in:
Roderick van Domburg 2022-01-06 09:43:50 +01:00
parent cc9a574b2e
commit 4ca1f661d5
No known key found for this signature in database
GPG key ID: FE2585E713F9F30A

View file

@ -1,8 +1,3 @@
use std::{
hint,
sync::atomic::{AtomicBool, Ordering},
};
use hyper::{Body, Method, Request}; use hyper::{Body, Method, Request};
use serde::Deserialize; use serde::Deserialize;
@ -40,7 +35,6 @@ impl Default for ApResolveData {
component! { component! {
ApResolver : ApResolverInner { ApResolver : ApResolverInner {
data: AccessPoints = AccessPoints::default(), data: AccessPoints = AccessPoints::default(),
in_progress: AtomicBool = AtomicBool::new(false),
} }
} }
@ -111,16 +105,6 @@ impl ApResolver {
} }
pub async fn resolve(&self, endpoint: &str) -> Result<SocketAddress, Error> { pub async fn resolve(&self, endpoint: &str) -> Result<SocketAddress, Error> {
// Use a spinlock to make this function atomic. Otherwise, various race conditions may
// occur, e.g. when the session is created, multiple components are launched almost in
// parallel and they will all call this function, while resolving is still in progress.
self.lock(|inner| {
while inner.in_progress.load(Ordering::Acquire) {
hint::spin_loop();
}
inner.in_progress.store(true, Ordering::Release);
});
if self.is_empty() { if self.is_empty() {
self.apresolve().await; self.apresolve().await;
} }
@ -140,7 +124,7 @@ impl ApResolver {
))) )))
} }
}; };
inner.in_progress.store(false, Ordering::Release);
Ok(access_point) Ok(access_point)
}) })
} }