Skip to content

Commit

Permalink
fix: deadlock on recursive emit_with_ack calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bastilimbach authored and 1c3t3a committed Nov 24, 2024
1 parent 2ef32ec commit d5f40d9
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions socketio/src/client/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,18 @@ impl RawClient {
return Ok(());
};

self.outstanding_acks.lock()?.retain_mut(|ack| {
if ack.id != id {
return true;
}
let outstanding_ack = {
let mut outstanding_acks = self.outstanding_acks.lock()?;
outstanding_acks
.iter()
.position(|ack| ack.id == id)
.map(|pos| outstanding_acks.remove(pos))
};

// If we found a matching ack, call its callback otherwise ignore it.
// The official implementation just removes the ack id on timeout:
// https://github.com/socketio/socket.io-client/blob/main/lib/socket.ts#L467-L495
if let Some(mut ack) = outstanding_ack {
if ack.time_started.elapsed() < ack.timeout {
if let Some(ref payload) = socket_packet.data {
ack.callback.deref_mut()(Payload::from(payload.to_owned()), self.clone());
Expand All @@ -293,10 +300,7 @@ impl RawClient {
}
}
}
// nope, just ignore it, the official implment just remove the ack id when timeout
// https://github.com/socketio/socket.io-client/blob/main/lib/socket.ts#L467-L495
false
});
}

Ok(())
}
Expand Down

1 comment on commit d5f40d9

@shenjackyuanjie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, that false was been removed

Please sign in to comment.