This commit is contained in:
NoLogicAlan committed 2026-05-03 21:37:15 +03:00
1 parent daa5a24dfb
commit fbc8f3d48a
1 file changed
+53 -57
+53 -57
View File
@@ -83,10 +83,6 @@ function isIgnorableMediaStateError(err) {
// ═══════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════
// PlayerWorkerPool — persistent worker pool to avoid spawning a new Node.js // PlayerWorkerPool — persistent worker pool to avoid spawning a new Node.js
// isolate (~40 MB V8 heap) for every search/play command. // isolate (~40 MB V8 heap) for every search/play command.
//
// Instead of: new Worker() per job → terminate after job (slow, memory spikes)
// Now: N workers stay alive, jobs are dispatched via postMessage with
// a unique jobKey so responses can be routed back to the right caller.
// ═══════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════
class PlayerWorkerPool { class PlayerWorkerPool {
@@ -315,6 +311,7 @@ export default class Player extends EventEmitter {
// Playback state // Playback state
leaving = false; leaving = false;
_paused = false; _paused = false;
_pausedAt = null; // track exact moment of pause for clock sync
_playingNext = false; _playingNext = false;
_isRecovering = false; _isRecovering = false;
startedPlaying = null; startedPlaying = null;
@@ -381,7 +378,7 @@ export default class Player extends EventEmitter {
// moonlink.js manager reference — injected by PlayerManager // moonlink.js manager reference — injected by PlayerManager
this._moonlink = opts.moonlink ?? null; this._moonlink = opts.moonlink ?? null;
// FIXED: Robust session sync that handles reconnection and stale sessions // Robust session sync that handles reconnection and stale sessions
if (this._moonlink) { if (this._moonlink) {
this._onMoonlinkReady = (sessionId) => { this._onMoonlinkReady = (sessionId) => {
const oldId = this._nl.sessionId; const oldId = this._nl.sessionId;
@@ -826,7 +823,6 @@ export default class Player extends EventEmitter {
const ffMsg = err?.message ?? String(err); const ffMsg = err?.message ?? String(err);
// treat SIGKILL as graceful — _stopMediaPlayer() always kills FFmpeg with SIGKILL. // treat SIGKILL as graceful — _stopMediaPlayer() always kills FFmpeg with SIGKILL.
// Previously this fell through to logger.error, creating a noisy false-positive.
const isGracefulKill = const isGracefulKill =
ffMsg.includes("aborted") || ffMsg.includes("aborted") ||
ffMsg.includes("Input stream error") || ffMsg.includes("Input stream error") ||
@@ -878,13 +874,14 @@ export default class Player extends EventEmitter {
return reject(e); return reject(e);
} }
// 20 min is plenty for any real track; 24h was holding the entire // Dynamic safety timeout based on track duration.
// stream closure (audioStream, MediaPlayer, Player ref) alive for a full const trackDuration = this._getTrackDurationMs(currentTrack);
// day whenever a stream dropped without firing "finish" (radio dropout, const safetyMs = (trackDuration > 0 && currentTrack.type !== "radio")
// network blip). Multiply by active guilds → gigabytes of leaked native mem. ? trackDuration + 15_000
const safetyMs = 20 * 60 * 1000; // 20 minutes : 20 * 60 * 1000; // default 20 min for radio/unknown
const safetyTimer = setTimeout(() => { const safetyTimer = setTimeout(() => {
logger.warn("[Player] Stream safety timeout — advancing queue"); logger.warn(`[Player] Stream safety timeout hit (${Math.round(safetyMs/1000)}s) — advancing queue`);
cleanup(); cleanup();
resolve(); resolve();
}, safetyMs); }, safetyMs);
@@ -1139,6 +1136,7 @@ export default class Player extends EventEmitter {
this.connection = null; this.connection = null;
this._mediaPlayer = null; this._mediaPlayer = null;
this._paused = false; this._paused = false;
this._pausedAt = null;
this._playingNext = false; this._playingNext = false;
this.leaving = false; this.leaving = false;
} catch (e) { } catch (e) {
@@ -1150,8 +1148,6 @@ export default class Player extends EventEmitter {
} }
destroy() { destroy() {
// guard against double-destroy — prevents listener accumulation and
// double-free of native LiveKit/revoice heap objects.
if (this._destroyed) return; if (this._destroyed) return;
this._destroyed = true; this._destroyed = true;
@@ -1163,28 +1159,19 @@ export default class Player extends EventEmitter {
this.leaving = true; this.leaving = true;
this._streamingStopped = true; this._streamingStopped = true;
this._stopInactivityTimer(); this._stopInactivityTimer();
// clear the searches map to free track data held in memory
this.searches.clear(); this.searches.clear();
// Terminate the persistent worker pool — releases the 2 worker isolates
// and their ~80 MB of V8 heap for this player.
if (this._workerPool) { if (this._workerPool) {
try { this._workerPool.terminate(); } catch (_) {} try { this._workerPool.terminate(); } catch (_) {}
this._workerPool = null; this._workerPool = null;
} }
// Drain queued worker slots so pending play/search calls resolve immediately.
while (this._workerQueue.length > 0) { while (this._workerQueue.length > 0) {
const resolve = this._workerQueue.shift(); const resolve = this._workerQueue.shift();
resolve(); resolve();
} }
this._workerSemaphore = 0; this._workerSemaphore = 0;
// Disconnect eagerly — don't defer with setTimeout so native LiveKit/
// revoice heap objects are released as soon as possible instead of
// waiting 400ms (which, multiplied by many concurrent destroys, left
// native WebRTC audio buffers alive far longer than necessary).
const connToDestroy = this.connection; const connToDestroy = this.connection;
this.connection = null; this.connection = null;
this._mediaPlayer = null; this._mediaPlayer = null;
@@ -1216,6 +1203,7 @@ export default class Player extends EventEmitter {
if (this._paused) if (this._paused)
return ":negative_squared_cross_mark: Already paused!"; return ":negative_squared_cross_mark: Already paused!";
this._paused = true; this._paused = true;
this._pausedAt = Date.now(); // Capture freeze moment
this._mediaPlayer?.pause(); this._mediaPlayer?.pause();
this.emit("playback", false); this.emit("playback", false);
this._stopInactivityTimer(); this._stopInactivityTimer();
@@ -1227,7 +1215,14 @@ export default class Player extends EventEmitter {
return ":negative_squared_cross_mark: There's nothing playing at the moment!"; return ":negative_squared_cross_mark: There's nothing playing at the moment!";
if (!this._paused) if (!this._paused)
return ":negative_squared_cross_mark: Not paused!"; return ":negative_squared_cross_mark: Not paused!";
// Adjust start time by the duration of the pause
if (this._pausedAt) {
this.startedPlaying += (Date.now() - this._pausedAt);
}
this._paused = false; this._paused = false;
this._pausedAt = null;
this._mediaPlayer?.resume(); this._mediaPlayer?.resume();
this.emit("playback", true); this.emit("playback", true);
this._stopInactivityTimer(); this._stopInactivityTimer();
@@ -1416,11 +1411,18 @@ export default class Player extends EventEmitter {
_createProgressBar(length = 15) { _createProgressBar(length = 15) {
const current = this.queue.getCurrent(); const current = this.queue.getCurrent();
if (!current?.duration || !this.startedPlaying) return Utils.progressBar(0, 1, length); if (!current?.duration || !this.startedPlaying) return Utils.progressBar(0, 1, length);
const elapsed = Date.now() - this.startedPlaying;
let totalMs; // Common elapsed calculation
if (typeof current.duration === "object") totalMs = (current.duration.seconds ?? 0) * 1000; const totalMs = this._getTrackDurationMs(current);
else if (typeof current.duration === "number") totalMs = current.duration; let elapsed = Date.now() - this.startedPlaying;
else return Utils.progressBar(0, 1, length); if (this._paused && this._pausedAt) {
elapsed = this._pausedAt - this.startedPlaying;
}
// Clamp to ensure 1:58/1:30 doesn't happen
if (totalMs > 0 && elapsed > totalMs) elapsed = totalMs;
elapsed = Math.max(0, elapsed);
const bar = Utils.progressBar(elapsed, totalMs, length); const bar = Utils.progressBar(elapsed, totalMs, length);
const timeNow = Utils.prettifyMS(elapsed); const timeNow = Utils.prettifyMS(elapsed);
const total = Utils.prettifyMS(totalMs); const total = Utils.prettifyMS(totalMs);
@@ -1457,23 +1459,32 @@ export default class Player extends EventEmitter {
let totalMs = 0; let totalMs = 0;
const current = this.queue.getCurrent(); const current = this.queue.getCurrent();
if (current?.duration && this.startedPlaying) { if (current?.duration && this.startedPlaying) {
const elapsed = Date.now() - this.startedPlaying; const totalMsCurrent = this._getTrackDurationMs(current);
const durationMs = typeof current.duration === "object" let elapsed = Date.now() - this.startedPlaying;
? (current.duration.seconds ?? 0) * 1000 if (this._paused && this._pausedAt) {
: current.duration; elapsed = this._pausedAt - this.startedPlaying;
totalMs += Math.max(0, durationMs - elapsed); }
totalMs += Math.max(0, totalMsCurrent - elapsed);
} }
for (const track of this.queue.data) { for (const track of this.queue.data) {
if (!track?.duration) continue; totalMs += this._getTrackDurationMs(track);
if (typeof track.duration === "object" && track.duration?.seconds) totalMs += track.duration.seconds * 1000;
else if (typeof track.duration === "number") totalMs += track.duration;
} }
return Utils.prettifyMS(totalMs); return Utils.prettifyMS(totalMs);
} }
getCurrentElapsedDuration() { getCurrentElapsedDuration() {
if (!this.startedPlaying) return "0:00"; if (!this.startedPlaying) return "0:00";
return Utils.prettifyMS(Date.now() - this.startedPlaying); const current = this.queue.getCurrent();
const totalMs = this._getTrackDurationMs(current);
let elapsed = Date.now() - this.startedPlaying;
if (this._paused && this._pausedAt) {
elapsed = this._pausedAt - this.startedPlaying;
}
// Clamp UI to total duration
if (totalMs > 0 && elapsed > totalMs) elapsed = totalMs;
return Utils.prettifyMS(Math.max(0, elapsed));
} }
list(page = 1, pageSize = 10) { list(page = 1, pageSize = 10) {
@@ -1581,7 +1592,6 @@ export default class Player extends EventEmitter {
_workerSemaphore = 0; _workerSemaphore = 0;
_workerMaxConcurrent = 3; _workerMaxConcurrent = 3;
_workerQueue = []; _workerQueue = [];
// Shared pool — one per Player instance (2 persistent workers, no per-job spawn overhead)
_workerPool = null; _workerPool = null;
_acquireWorkerSlot() { _acquireWorkerSlot() {
@@ -1604,8 +1614,6 @@ export default class Player extends EventEmitter {
_getWorkerPool() { _getWorkerPool() {
if (!this._workerPool) { if (!this._workerPool) {
const workerPath = new URL("./worker.mjs", import.meta.url); const workerPath = new URL("./worker.mjs", import.meta.url);
// 2 persistent workers per player — enough for concurrent play+search
// without spawning a new 40MB isolate per command.
this._workerPool = new PlayerWorkerPool(2, workerPath); this._workerPool = new PlayerWorkerPool(2, workerPath);
} }
return this._workerPool; return this._workerPool;
@@ -1681,7 +1689,7 @@ export default class Player extends EventEmitter {
if (!data) { if (!data) {
logger.worker("[Player] Worker returned empty result"); logger.worker("[Player] Worker returned empty result");
events.emit("message", "Could not load track - no data returned."); events.emit("message", "Could not load track - no data returned.");
events.removeAllListeners(); // release closure references immediately events.removeAllListeners();
return; return;
} }
@@ -1706,8 +1714,6 @@ export default class Player extends EventEmitter {
if (!this.queue.getCurrent()) { if (!this.queue.getCurrent()) {
this.playNext(); this.playNext();
} }
// Drop all listeners — the job is done, nothing more will be emitted.
// This releases the closure the command holds over statusMsg etc.
events.removeAllListeners(); events.removeAllListeners();
}) })
.catch((reason) => { .catch((reason) => {
@@ -1771,10 +1777,6 @@ export default class Player extends EventEmitter {
async playNext() { async playNext() {
if (this._playingNext) return; if (this._playingNext) return;
this._playingNext = true; this._playingNext = true;
// use try/finally so _playingNext is ALWAYS cleared, even if _doPlayNext
// throws. Previously _doPlayNext would set _playingNext=false itself before
// tail-calling this.playNext(), causing a race where the outer finally would
// then overwrite the new call's _playingNext=true with false.
try { await this._doPlayNext(); } try { await this._doPlayNext(); }
finally { this._playingNext = false; } finally { this._playingNext = false; }
} }
@@ -1831,9 +1833,6 @@ export default class Player extends EventEmitter {
if (songData) { if (songData) {
this.queue.data.unshift(songData); this.queue.data.unshift(songData);
this.queue.current = null; this.queue.current = null;
// _playingNext is intentionally NOT cleared here — the outer
// playNext() finally block handles it. Recovery will call playNext()
// after the outer finally has run and cleared the flag.
} }
this._recoverConnection(); this._recoverConnection();
return; return;
@@ -1903,15 +1902,16 @@ export default class Player extends EventEmitter {
logger.error("[Player] No valid stream URL for:", songData.title); logger.error("[Player] No valid stream URL for:", songData.title);
this.emit("message", mkEmbed(`:x: Could not get stream URL for **${songData.title}** — skipping...`)) this.emit("message", mkEmbed(`:x: Could not get stream URL for **${songData.title}** — skipping...`))
this._streamingStopped = false; this._streamingStopped = false;
// tail-call via _doPlayNext directly instead of manipulating
// _playingNext and calling playNext() — avoids the race where the outer
// playNext() finally overwrites _playingNext=true set by the new call.
return this._doPlayNext(); return this._doPlayNext();
} }
logger.player(`[Player:${this._guildId}] Streaming: ${songData.title}`); logger.player(`[Player:${this._guildId}] Streaming: ${songData.title}`);
// Reset all timing flags
this.startedPlaying = Date.now(); this.startedPlaying = Date.now();
this._paused = false; this._paused = false;
this._pausedAt = null;
if (songData.type !== "radio" || !this._radioAnnounced) { if (songData.type !== "radio" || !this._radioAnnounced) {
this.announceSong(songData); this.announceSong(songData);
if (songData.type === "radio") this._radioAnnounced = true; if (songData.type === "radio") this._radioAnnounced = true;
@@ -1936,15 +1936,11 @@ export default class Player extends EventEmitter {
this.queue.current = null; this.queue.current = null;
this._streamingStopped = false; this._streamingStopped = false;
await Utils.sleep(1500); await Utils.sleep(1500);
// tail-call directly into _doPlayNext instead of setting
// _playingNext=false and calling playNext() — eliminates the race where
// the outer finally resets _playingNext=false mid-execution of the new call.
if (!this.leaving && !this._skipping) return this._doPlayNext(); if (!this.leaving && !this._skipping) return this._doPlayNext();
} else { } else {
if (!this._paused) { if (!this._paused) {
if (!this.queue.songLoop) this.queue.current = null; if (!this.queue.songLoop) this.queue.current = null;
this._streamingStopped = false; this._streamingStopped = false;
// same tail-call fix as above
return this._doPlayNext(); return this._doPlayNext();
} }
} }