Possible Memory Leak in StreamDispatcher
See original GitHub issuePlease describe the problem you are having in as much detail as possible: I’m thinking that there maybe a leak on Discord’s.js voice in Master. In 50 Hours of Uptime, My bot is now eating 12gb of ram, and it continues to rise as new dispatchers are being created.
I am fully aware that Music Bots tend to eat ram, but isn’t this quite overboard?
Ram Usage Below over 50 hours of uptime. https://cdn.discordapp.com/attachments/450644221410410506/490741735501201418/Untitled.png
Include a reproducible code sample here, if possible:
class KashimaPlayer {
constructor(client, guildID) {
this.client = client;
this.guildID = guildID;
this.queue = this.client.queue.get(this.guildID);
this.guildObj = this.client.guilds.get(this.guildID);
this.voiceConnection = this.client.voiceConnections.get(this.guildID);
this.textChannel = this.guildObj.channels.get(this.queue.textChannel);
this.stream;
this.dispatcher;
};
async start() {
if (!this.queue) {
this.voiceConnection.disconnect();
return;
} else if (!this.queue.songs.length) {
try {
await this.textChannel.send('Stream Ended');
} catch (error) {
this.client.cannons.fire(this.client, error);
} finally {
this.client.queue.delete(this.guildID);
this.voiceConnection.disconnect();
return;
};
};
let info;
let error;
let songlength;
try {
info = await this.client.ytdl.getBasicInfo(this.queue.songs[0].url);
songlength = parseInt(info.length_seconds);
if (songlength !== 0 && songlength <= 1800) {
this.stream = this.client.ytdl(this.queue.songs[0].url, { quality: 'highestaudio' });
this.dispatcher = this.voiceConnection.play(this.stream, { passes: 3 });
} else {
this.queue.songs.shift();
await this.start();
await this.textChannel.send('\:negative_squared_cross_mark: Teitoku, This Video is a **livestream or longer than 30 minutes**. Hence I **cannot play it**.');
return;
};
} catch (error) {
this.queue.songs.shift();
// this.client.cannons.fire(this.client, error); this line makes the logger go wild. Lmao.
await this.start();
await this.textChannel.send(`\:warning: Teitoku-san, this **song is unplayable**. Because\`\`\`${error}\`\`\``);
return;
};
this.stream.on('error', (err) => {
error = 1;
this.dispatcher.end();
this.textChannel.send(`\:negative_squared_cross_mark: Teitoku-san, **I encountered an error** while playing the song. Here is the Report.\`\`\`${err}\`\`\``)
.catch(e => this.client.cannons.fire(this.client, e));
});
this.dispatcher.on('start', async () => {
try {
await this.textChannel.send({
embed: {
color: 0xc0c0c0,
thumbnail: {
url: this.queue.songs[0].thumbnail
},
fields: [{
name: "\:musical_note: Now Playing",
value: `**${await this.parse(`${songlength || 0}`)}** | [${this.queue.songs[0].title}](${this.queue.songs[0].url})`
}, {
name: "\:inbox_tray: Queued by",
value: `**@${(await this.client.users.fetch(this.queue.songs[0].memberId)).tag}**`
}
],
timestamp: new Date(),
footer: {
icon_url: this.guildObj.iconURL({format: 'png', size: 512}) || 'https://i.imgur.com/CjcQBE6.jpg',
text: this.guildObj.name
}
}
});
} catch (error) {
this.client.cannons.fire(this.client, error);
};
});
this.dispatcher.on('finish', async () => {
try {
this.queue.songs.shift();
await this.destroy();
} catch (err) {
this.client.cannons.fire(this.client, err);
} finally {
try {
await this.start();
} catch (err) {
this.client.cannons.fire(this.client, err);
};
};
});
this.dispatcher.on('error', (error) => {
if (!error) {
this.dispatcher.end();
this.textChannel.send(`\:negative_squared_cross_mark: Teitoku-san, **I encountered an error** while playing the song. Here is the Report.\`\`\`${error}\`\`\``)
.catch(e => this.client.cannons.fire(this.client, e));
};
});
};
async destroy() {
this.dispatcher.removeAllListeners();
this.stream.removeAllListeners();
this.dispatcher.destroy();
this.stream.destroy();
};
async parse(seconds) {
return ( seconds - (seconds %= 60) ) / 60 + (9 < seconds ? ':' : ':0') + seconds
};
};
Further details:
-
discord.js version: Discord.js Master
-
Node.js version: 10.9.0
-
Operating system: Ubuntu 16.04 LTS
-
Priority this issue should have – please be realistic and elaborate if possible: Medium, Due to the fact that it will keep on increasing the memory usage over time.
-
I found this issue while running code on a user account
-
I have also tested the issue on latest master, commit hash:
c63f15c8bc98145c3e04955229648d69dbdf1678
Issue Analytics
- State:
- Created 5 years ago
- Comments:23 (16 by maintainers)
Hi, sorry to interject, but it appears this issue has devolved into support chat. There exists a lovely platform for support chat here: https://discord.gg/bRCvFy9 It would mean a lot to everyone who subscribes to this repo to not get every message you are exchanging here as an email. This space is reserved for issues and feature requests, not questions or support chat.
I just decided to go with Lavalink in my rewrite. It’s still leaking but not as much as it was before. Thanks for those who participated and gave me tips in fixing this issue.