diff --git a/.cspell.json b/.cspell.json index 10a73c7821..ad88a31fff 100644 --- a/.cspell.json +++ b/.cspell.json @@ -100,7 +100,8 @@ "webfonts", "websockets", "wifi", - "xsalsa20" + "xsalsa20", + "Alives" ], "flagWords": [], "ignorePaths": [ diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 3df90f8476..91577e484c 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -138,6 +138,17 @@ export class Waku { this.startKeepAlive(connection.remotePeer, pingKeepAlive, relayKeepAlive); }); + /** + * NOTE: Event is not being emitted on closing nor losing a connection. + * @see https://github.com/libp2p/js-libp2p/issues/939 + * @see https://github.com/status-im/js-waku/issues/252 + * + * >This event will be triggered anytime we are disconnected from another peer, + * >regardless of the circumstances of that disconnection. + * >If we happen to have multiple connections to a peer, + * >this event will **only** be triggered when the last connection is closed. + * @see https://github.com/libp2p/js-libp2p/blob/bad9e8c0ff58d60a78314077720c82ae331cc55b/doc/API.md?plain=1#L2100 + */ libp2p.connectionManager.on("peer:disconnect", (connection: Connection) => { this.stopKeepAlive(connection.remotePeer); }); @@ -296,7 +307,8 @@ export class Waku { } async stop(): Promise { - return this.libp2p.stop(); + this.stopAllKeepAlives(); + await this.libp2p.stop(); } /** @@ -460,6 +472,18 @@ export class Waku { delete this.relayKeepAliveTimers[peerIdStr]; } } + + private stopAllKeepAlives(): void { + for (const timer of [ + ...Object.values(this.pingKeepAliveTimers), + ...Object.values(this.relayKeepAliveTimers), + ]) { + clearInterval(timer); + } + + this.pingKeepAliveTimers = {}; + this.relayKeepAliveTimers = {}; + } } const awaitTimeout = (ms: number, rejectReason: string): Promise =>