From 0df724a58f2b8760ac438f5ebf2e5791fa22d2d6 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 9 Mar 2023 16:30:37 +0100 Subject: [PATCH] node: Bind control socket immediately This avoids race conditions, especially in tests, where we try to connect to the node right after spawning it, but since the `Runtime::run` function is often called in a different thread, the socket is not created in time. --- radicle-node/src/runtime.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/radicle-node/src/runtime.rs b/radicle-node/src/runtime.rs index 2c424b97..cb9e9585 100644 --- a/radicle-node/src/runtime.rs +++ b/radicle-node/src/runtime.rs @@ -73,6 +73,7 @@ pub enum Error { pub struct Runtime { pub id: NodeId, pub home: Home, + pub control: UnixListener, pub handle: Handle, pub storage: Storage, pub reactor: Reactor>, @@ -168,10 +169,20 @@ impl Runtime { atomic, }, ); + let control = match UnixListener::bind(home.socket()) { + Ok(sock) => sock, + Err(err) if err.kind() == io::ErrorKind::AddrInUse => { + return Err(Error::AlreadyRunning(home.socket())); + } + Err(err) => { + return Err(err.into()); + } + }; Ok(Runtime { id, home, + control, storage, reactor, daemon, @@ -188,18 +199,9 @@ impl Runtime { log::info!(target: "node", "Running node {} in {}..", self.id, home.path().display()); log::info!(target: "node", "Binding control socket {}..", home.socket().display()); - let listener = match UnixListener::bind(home.socket()) { - Ok(sock) => sock, - Err(err) if err.kind() == io::ErrorKind::AddrInUse => { - return Err(Error::AlreadyRunning(home.socket())); - } - Err(err) => { - return Err(err.into()); - } - }; let control = thread::Builder::new().name(self.id.to_human()).spawn({ let handle = self.handle.clone(); - move || control::listen(listener, handle) + move || control::listen(self.control, handle) })?; let _signals = thread::Builder::new() .name(self.id.to_human())