fix concurrency: resolve deadlock and lock ordering in P2P shutdown and

peer mapping
This commit is contained in:
2026-02-11 14:19:09 -03:00
parent 4c64c4cc0a
commit 88a2abe521
3 changed files with 55 additions and 21 deletions

View File

@@ -484,11 +484,14 @@ pub fn shutdownImpl(p2p: *P2P) void {
p2p.state_mutex.unlock();
// Clear event queue
p2p.event_mutex.lock();
defer p2p.event_mutex.unlock();
p2p.event_queue.clearRetainingCapacity();
{
p2p.event_mutex.lock();
defer p2p.event_mutex.unlock();
p2p.event_queue.clearRetainingCapacity();
}
// Emit final event (ignore errors during shutdown)
// Must be outside event_mutex scope — emitEvent takes the lock internally
p2p.emitEvent(P2PEvent.create(.library_stopped, .library_stopped)) catch |err| {
std.log.warn("Failed to emit shutdown event: {}", .{err});
};