Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Avoid race condition in address_map
Browse files Browse the repository at this point in the history
The check for being connected and sending a message is not atomic. To
not trigger a panic, we need to attempt sending the message and react to
the error accordingly.

Fixes #1049.
  • Loading branch information
thomaseizinger committed Jan 10, 2022
1 parent 2285225 commit a0ece19
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions daemon/src/address_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ where
M: Message<Result = ()>,
A: Handler<M> + ActorName,
{
match self.inner.get(key) {
Some(addr) if addr.is_connected() => {
addr.send(msg)
.await
.expect("we checked that we are connected");
Ok(())
}
_ => Err(NotConnected::new::<A>()),
}
self.get(key)?
.send(msg)
.await
.map_err(|_| NotConnected::new::<A>())?;

Ok(())
}

/// Sends a message to the actor stored with the given key.
Expand All @@ -71,16 +68,20 @@ where
M: Message<Result = anyhow::Result<()>>,
A: Handler<M> + ActorName,
{
match self.inner.get(key) {
Some(addr) if addr.is_connected() => {
let res = addr
.send(msg)
.await
.expect("we checked that we are connected");
Ok(res)
}
_ => Err(NotConnected::new::<A>()),
}
let result = self
.get(key)?
.send(msg)
.await
.map_err(|_| NotConnected::new::<A>())?;

Ok(result)
}

fn get(&self, key: &K) -> Result<&Address<A>, NotConnected>
where
A: ActorName,
{
self.inner.get(key).ok_or_else(|| NotConnected::new::<A>())
}
}

Expand Down

0 comments on commit a0ece19

Please sign in to comment.