Skip to content

Commit

Permalink
fix recursive destructor (thanks to harrm for finding that bug)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
  • Loading branch information
turuslan authored and igor-egorov committed Feb 17, 2025
1 parent 4fdc79c commit 168106b
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions core/network/notifications/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,27 @@ namespace kagome::network::notifications {
if (not peer) {
return;
}
peer.insert_or_assign(PeerOutBackoff{
scheduler_->scheduleWithHandle(
[WEAK_SELF, peer_id] {
WEAK_LOCK(self);
self->onBackoff(peer_id);
},
backoffTime()),
});
/*
there was bug (created by turuslan, found by harrm) causing double free.
*peer = PeerOutBackoff
variant assignment destroys old contained value then inserts new.
`~PeerOutConnected` calls `YamuxStream::reset`,
`YamuxStream::reset` cancels pending read,
read error calls `onError`,
`onError` calls `backoff`,
but `peer` still contains half-destructed `PeerOutConnected`,
and calls `~PeerOutConnected` again,
causing double free.
*/
std::exchange(*peer,
PeerOutBackoff{
scheduler_->scheduleWithHandle(
[WEAK_SELF, peer_id] {
WEAK_LOCK(self);
self->onBackoff(peer_id);
},
backoffTime()),
});
}

void Protocol::onBackoff(const PeerId &peer_id) {
Expand Down

0 comments on commit 168106b

Please sign in to comment.