From 8318b07d89414df3479652383e9dcad185910bc4 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 19 Jan 2023 09:43:47 +0100 Subject: [PATCH] node: Fix spurious test failures In some circumstances, the nodes are already offline when the objects are dropped. Signed-off-by: Alexis Sellier --- radicle-node/src/client/handle.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/radicle-node/src/client/handle.rs b/radicle-node/src/client/handle.rs index fba2c243..75448835 100644 --- a/radicle-node/src/client/handle.rs +++ b/radicle-node/src/client/handle.rs @@ -1,5 +1,6 @@ use std::io::{self, Write}; use std::os::unix::net::UnixStream; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use crossbeam_channel as chan; @@ -56,6 +57,9 @@ impl From> for Error { pub struct Handle { pub(crate) home: Home, pub(crate) controller: reactor::Controller>, + + /// Whether a shutdown was initiated or not. Prevents attempting to shutdown twice. + shutdown: Arc, } impl Clone for Handle { @@ -63,13 +67,18 @@ impl Clone for Handle { Self { home: self.home.clone(), controller: self.controller.clone(), + shutdown: self.shutdown.clone(), } } } impl Handle { pub fn new(home: Home, controller: reactor::Controller>) -> Self { - Self { home, controller } + Self { + home, + controller, + shutdown: Arc::default(), + } } pub fn worker_result(&mut self, resp: WorkerResp) -> Result<(), Error> { @@ -182,6 +191,14 @@ impl radicle::node::Handle for Handle { } fn shutdown(self) -> Result<(), Error> { + // If the current value is `false`, set it to `true`, otherwise error. + if self + .shutdown + .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) + .is_err() + { + return Ok(()); + } // Send a shutdown request to our own control socket. This is the only way to kill the // control thread gracefully. Since the control thread may have called this function, // the control socket may already be disconnected. Ignore errors.