question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible Memory Leak in StreamDispatcher

See original GitHub issue

Please 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:closed
  • Created 5 years ago
  • Comments:23 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
bdistincommented, Sep 20, 2018

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.

0reactions
Deivucommented, Sep 26, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible memory leak in StreamDispatcher #4287 - GitHub
The bot is continuously (every X seconds) streaming a different Youtube video with a random seek to a voice channel. The memory (heap...
Read more >
Possible Memory leak in FFMPEGAutogen Example
I'm using the https://github.com/Ruslan-B/FFmpeg.AutoGen library to connect to an rtsp camera stream and save the frames as png images.
Read more >
Node.js Memory Leak Detection: How to Debug & Avoid Them
Learn what Node.js memory leaks are. Discover common causes and go through the whole debugging process, from detection to fixing and ...
Read more >
4 Types of Memory Leaks in JavaScript and How to Get Rid Of ...
Learn about memory leaks in JavaScript and what can be done to solve it! ... it is possible to leak memory in a...
Read more >
Debugging Memory Leaks in Node.js Applications - Toptal
Memory leaks in long running Node.js applications are like ticking time bombs that, if left unchecked in production environments, can result in devastating ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found