EventEmitter memory leak detected

This commit is contained in:
NoLogicAlan committed 2026-08-29 11:48:49 +03:00
1 parent a6ac14aff1
commit 0c1f9adb12
1 file changed
+34
+34
View File
@@ -27,6 +27,9 @@ const WEBM_MAGIC = Buffer.from([0x1a, 0x45, 0xdf, 0xa3]);
/** @type {Buffer} @description OGG container magic bytes ("OggS"). */
const OGG_MAGIC = Buffer.from("OggS", "ascii");
/** @type {number} @description livekit TrackKind.KIND_AUDIO — publications with this kind are managed by the bridge. */
const TRACK_KIND_AUDIO = 1;
/** @extends {EventEmitter} */
export class FluxerAudioBridge extends EventEmitter {
_conn = null;
@@ -77,6 +80,8 @@ export class FluxerAudioBridge extends EventEmitter {
this.stop();
if (!conn) throw new Error("[AudioBridge] No voice connection provided");
this._unpublishStaleAudioTracks(conn);
const generation = ++this._playGeneration;
this._conn = conn;
this._playing = true;
@@ -576,6 +581,7 @@ export class FluxerAudioBridge extends EventEmitter {
if (this._conn) {
try { this._conn.stop(); } catch (_) {}
this._unpublishStaleAudioTracks(this._conn);
}
if (wasPlaying) {
@@ -584,6 +590,34 @@ export class FluxerAudioBridge extends EventEmitter {
}
}
/**
* @param {object} conn - Fluxer voice connection
* @param {string|null} [keepSid=null] - Track SID to keep (currently active publication)
* @private
*/
_unpublishStaleAudioTracks(conn, keepSid = null) {
try {
const room = conn?.room;
const participant = room?.localParticipant;
if (!room?.isConnected || typeof participant?.unpublishTrack !== "function") return;
const publications = participant.trackPublications;
if (!publications || typeof publications.entries !== "function") return;
for (const [sid, pub] of publications.entries()) {
if (keepSid && sid === keepSid) continue;
if (pub?.kind !== TRACK_KIND_AUDIO) continue;
try {
const p = participant.unpublishTrack(sid, true);
if (p?.catch) p.catch(() => {});
logger.player("[AudioBridge] Unpublished stale audio track: " + sid);
} catch (_) {}
}
} catch (_) {
// Never let cleanup break playback.
}
}
/**
* Set the playback volume on the voice connection.
* @param {number} percent - Volume 0–100.