From f2315d34c56d60ba370d7f20389579cbeb8b77db Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 29 Jul 2025 20:29:53 +0000 Subject: [PATCH] Fix memory leaks and improve resource cleanup in player connection Co-authored-by: makvanaparas564 --- src/Player/Connection.ts | 22 ++++++++++++++++++++-- src/Player/CustomFilters.ts | 6 +++++- src/Player/Player.ts | 7 +++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Player/Connection.ts b/src/Player/Connection.ts index 4447793a..db318826 100644 --- a/src/Player/Connection.ts +++ b/src/Player/Connection.ts @@ -54,6 +54,7 @@ export class Connection { public voice: IVoiceServer | PartialNull; public self_mute: boolean; public self_deaf: boolean; + private unpauseTimeout: NodeJS.Timeout | null; /** * The connection class @@ -70,6 +71,7 @@ export class Connection { }; this.self_mute = false; this.self_deaf = false; + this.unpauseTimeout = null; } /** @@ -87,12 +89,18 @@ export class Connection { guildId: this.player.guildId, data: { voice: this.voice }, }); - setTimeout(async () => { + + // Clear any existing timeout to prevent memory leaks + if (this.unpauseTimeout) { + clearTimeout(this.unpauseTimeout); + } + + this.unpauseTimeout = setTimeout(async () => { await this.player.node.rest.updatePlayer({ guildId: this.player.guildId, data: { paused: false }, }) - + this.unpauseTimeout = null; }, 1000) this.player.poru.emit( @@ -120,4 +128,14 @@ export class Connection { this.self_mute = self_mute; this.voice.sessionId = session_id || null; }; + + /** + * Clean up connection resources + */ + public cleanup(): void { + if (this.unpauseTimeout) { + clearTimeout(this.unpauseTimeout); + this.unpauseTimeout = null; + } + }; }; \ No newline at end of file diff --git a/src/Player/CustomFilters.ts b/src/Player/CustomFilters.ts index af175bb3..a94ca3a8 100644 --- a/src/Player/CustomFilters.ts +++ b/src/Player/CustomFilters.ts @@ -56,7 +56,11 @@ export class customFilter extends Filters { if (!this.player) return this; this.slowmode = val; - await this.setFilters(val ? { timescale: { speed: 0.5, pitch: 1.0, rate: 0.8 } } as FiltersOptions : await this.clearFilters()); + if (val) { + await this.setFilters({ timescale: { speed: 0.5, pitch: 1.0, rate: 0.8 } } as FiltersOptions); + } else { + await this.clearFilters(); + } return this; }; diff --git a/src/Player/Player.ts b/src/Player/Player.ts index dd25c71d..cddc008c 100644 --- a/src/Player/Player.ts +++ b/src/Player/Player.ts @@ -12,8 +12,10 @@ type Loop = "NONE" | "TRACK" | "QUEUE" const escapeRegExp = (str: string) => { try { - str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") - } catch { } + return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + } catch { + return str + } } interface BaseVoiceReceiverEvent { @@ -464,6 +466,7 @@ export class Player extends EventEmitter { */ public async destroy(): Promise { await this.disconnect() + this.connection.cleanup() // Clean up connection resources await this.node.rest.destroyPlayer(this.guildId) this.poru.emit("debug", this.guildId, `[Poru Player] destroyed the player`) this.poru.emit("playerDestroy", this)