fix: avoid hang on Waku node stop

Signed-off-by: kblinichkin <kirill.blinichkin@gmail.com>
This commit is contained in:
kblinichkin 2026-06-08 12:14:08 -05:00 committed by Igor Sirotin
parent 81291ef862
commit 6821697534

View File

@ -527,42 +527,69 @@ func (w *WakuNode) Stop() {
return
}
w.bcaster.Stop()
// Cancel the node context before stopping protocols: once processLoop sees
// ctx.Done() it stops draining its channels, so a later send would block.
w.cancel()
stopWithTimeout(w.bcaster.Stop, "bcaster", 5*time.Second, w.log)
defer w.connectionNotif.Close()
defer w.addressChangesSub.Close()
w.host.Network().StopNotify(w.connectionNotif)
w.relay.Stop()
w.lightPush.Stop()
w.legacyStore.Stop()
w.filterFullNode.Stop()
w.filterLightNode.Stop()
// Each Stop() calls wg.Wait() internally and can block on a goroutine stuck
// writing to a dead connection; the timeout keeps Stop() bounded.
stopWithTimeout(w.relay.Stop, "relay", 10*time.Second, w.log)
stopWithTimeout(w.lightPush.Stop, "lightPush", 5*time.Second, w.log)
stopWithTimeout(w.legacyStore.Stop, "legacyStore", 5*time.Second, w.log)
stopWithTimeout(w.filterFullNode.Stop, "filterFullNode", 5*time.Second, w.log)
stopWithTimeout(w.filterLightNode.Stop, "filterLightNode", 5*time.Second, w.log)
if w.opts.enableDiscV5 {
w.discoveryV5.Stop()
stopWithTimeout(w.discoveryV5.Stop, "discoveryV5", 5*time.Second, w.log)
}
w.peerExchange.Stop()
w.rendezvous.Stop()
stopWithTimeout(w.peerExchange.Stop, "peerExchange", 5*time.Second, w.log)
stopWithTimeout(w.rendezvous.Stop, "rendezvous", 5*time.Second, w.log)
stopWithTimeout(w.peerConnector.Stop, "peerConnector", 5*time.Second, w.log)
stopWithTimeout(func() { _ = w.stopRlnRelay() }, "rlnRelay", 5*time.Second, w.log)
stopWithTimeout(w.timesource.Stop, "timesource", 5*time.Second, w.log)
w.peerConnector.Stop()
_ = w.stopRlnRelay()
w.timesource.Stop()
w.host.Close()
w.cancel()
w.wg.Wait()
// Bound the wait: goroutines select on ctx.Done() and should exit promptly.
waitDone := make(chan struct{})
go func() { w.wg.Wait(); close(waitDone) }()
select {
case <-waitDone:
case <-time.After(5 * time.Second):
w.log.Error("timed out waiting for node goroutines to stop")
}
close(w.enrChangeCh)
// host.Close() can block indefinitely draining dead sockets; bound it too.
hostClosed := make(chan struct{})
go func() { w.host.Close(); close(hostClosed) }()
select {
case <-hostClosed:
case <-time.After(5 * time.Second):
w.log.Warn("timed out waiting for host.Close(); proceeding")
}
w.cancel = nil
}
// stopWithTimeout runs fn and returns once it completes or timeout elapses,
// logging a warning on timeout so one stuck component cannot hang Stop().
func stopWithTimeout(fn func(), name string, timeout time.Duration, log *zap.Logger) {
done := make(chan struct{})
go func() { fn(); close(done) }()
select {
case <-done:
case <-time.After(timeout):
log.Warn("timed out stopping component", zap.String("component", name))
}
}
// Host returns the libp2p Host used by the WakuNode
func (w *WakuNode) Host() host.Host {
return w.host