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.
This commit is contained in:
Alexis Sellier 2023-03-09 16:30:37 +01:00
parent adbf6f5bf5
commit 0df724a58f
No known key found for this signature in database
1 changed files with 12 additions and 10 deletions

View File

@ -73,6 +73,7 @@ pub enum Error {
pub struct Runtime<G: Signer + Ecdh> {
pub id: NodeId,
pub home: Home,
pub control: UnixListener,
pub handle: Handle<G>,
pub storage: Storage,
pub reactor: Reactor<wire::Control<G>>,
@ -168,10 +169,20 @@ impl<G: Signer + Ecdh + 'static> Runtime<G> {
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<G: Signer + Ecdh + 'static> Runtime<G> {
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())