Skip to content

Commit f0efac5

Browse files
authored
fix recursive destructor (#2380)
Signed-off-by: turuslan <[email protected]>
1 parent 337f063 commit f0efac5

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

core/network/notifications/protocol.cpp

+21-8
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,27 @@ namespace kagome::network::notifications {
184184
if (not peer) {
185185
return;
186186
}
187-
peer.insert_or_assign(PeerOutBackoff{
188-
scheduler_->scheduleWithHandle(
189-
[WEAK_SELF, peer_id] {
190-
WEAK_LOCK(self);
191-
self->onBackoff(peer_id);
192-
},
193-
backoffTime()),
194-
});
187+
/*
188+
there was bug causing double free.
189+
*peer = PeerOutBackoff
190+
variant assignment destroys old contained value then inserts new.
191+
`~PeerOutOpen` calls `YamuxStream::reset`,
192+
`YamuxStream::reset` cancels pending read,
193+
read error calls `onError`,
194+
`onError` calls `backoff`,
195+
but `peer` still contains half-destructed `PeerOutOpen`,
196+
and calls `~PeerOutOpen` again,
197+
causing double free.
198+
*/
199+
std::exchange(*peer,
200+
PeerOutBackoff{
201+
scheduler_->scheduleWithHandle(
202+
[WEAK_SELF, peer_id] {
203+
WEAK_LOCK(self);
204+
self->onBackoff(peer_id);
205+
},
206+
backoffTime()),
207+
});
195208
}
196209

197210
void Protocol::onBackoff(const PeerId &peer_id) {

0 commit comments

Comments
 (0)