Node-ytdl-core: Random ECONNRESET

Created on 10 Jul 2016  路  52Comments  路  Source: fent/node-ytdl-core

Hello,

I've had a issue with this library ever since I started to use it. I basically pass the ytdl stream into ffmpeg to extract the audio from the video. Sometimes the stream crashes with an ECONNRESET, and i have no idea why.

Stacktrace:
Error: read ECONNRESET at exports._errnoException (util.js:1007:11) at TLSWrap.onread (net.js:563:26)

bug

Most helpful comment

Hey guys, he's actually currently working on a fix for it so we can stop confirming this, as the PR #128 should fix it.

All 52 comments

Do you have an example where this happens?

Is the example here similar?

@fent This is where it happens.

Got that error, too. No specific Video or something just happened - and nothing can be downloaded.

There's no need to call ytdlStream.end() btw, .destroy() should be enough in theory.

Have been getting this error for a few months now...

I have noticed that using the audioonly flag actually helps. Doesn't throw that error so often anymore.

Does this happen at the start of the download, middle, end?

Mainly the end, around 15 seconds before the end of the video

Happening to me too.

I have the same issue, but it happens to me in the middle / first half of the video

I've noticed the issue occur sporadically, sometimes I can go a few videos with nothing while others cut off half way or near the end of playback.

Yeah I think it is pretty much random. But for me it's more often at the beginning of a video (most times)

Can also report this issue. Happens randomly through playback, just started happening recently. I've always had this with connection loss, but the bot is currently not losing connection and it's just happening while the song was playing fine. Seems to be more common in the past few days.

@fent People are saying it could be due to the fact you are downloading videos "unofficially", or without a developer key.

Same over here, random but mostly after the middle of the video

I'm still having issues with this in #128 even though the pull marked it as fixed.

I can also confirm that this issue is still occurring at roughly the same frequency.

I can confirm as well.

Can confirm that this is still occurring about halfway through the stream.

I almost always get this error at 1:23 when I try to stream this video: https://www.youtube.com/watch?v=iRSuh7OVdDs

This error occurs for me with every video longer than ~1min, but I found that that video throws the error fairly consistently.

Can confirm, this is still occurring.

Can also confirm.

Hey guys, he's actually currently working on a fix for it so we can stop confirming this, as the PR #128 should fix it.

Related to this issue. Was getting the reset error, but I manually pulled the new commits (used the default npm installer previously). Reconnection seems to not work correctly, the video doesn't end correctly. Audio goes straight to the discord voice connector. Used this snippet of code (and modified) you provided on another thread

var ytdl = require('..');
var stream = ytdl('https://www.youtube.com/watch?v=6ySOK70gFPk');


var size;
stream.on('response', function(res) {
  size = res.headers['content-length'];
  console.log('size', size);
});

var dataEmitted = 0;
stream.on('data', function(chunk) {
  dataEmitted += chunk.length;
  console.log('on data', chunk.length, dataEmitted);
});

stream.on('end', function() {
  console.log('emitted', dataEmitted);
});

This was the result.
log.txt
The video that was causing it: https://www.youtube.com/watch?v=aatr_2MstrI

@ibexes did u try with the latest reg release(v11)?
u should also add a listener for the error event if ur having problems...

@TimeForANinja I believe so, yes. Previously it wouldn't even reconnect, it'd just throw the error. Now it just doesn't reconnect properly.

Try again with the master branch please
ran perfectly fine for me with reconnects

@TimeForANinja I do have a listener for errors, there apparently isn't an error and it just hangs at the last line of the log, instead of finishing and giving me an emitted. I'm filtering the stream for audio only if that makes a difference.

I've isolated the code and it runs fine, in a single block. I'm using the stream with the playstream function in discord voice connection, so that could explain why there is an error, but I'm not sure why it hangs. It's only on a few random videos but they seem consistent and stop at the same location. I'm not sure how to recreate the environment in the isolated code since I don't know how the playstream function works exactly. I'll add a timestamp of the log and see the rate.
log.txt

The 'ended' at the end is printed when the discord playstream function has ended.
E: using the master branch, renamed the package something random to make sure it's using it correctly.
E2: when it reconnects, it doesn't seem to reconnect at the correct location with discord.
E3: it doesn't always print ended
E4: 'reason: Stream is not generating quickly enough.' when I logged why playstream ended. Awkward, not really sure how to fix that...

E5:
log.txt emitted at end =/= size at top

Works perfectly on my end. I get 'reason: Stream is not generating quickly enough.' when I reach the end of the video, so no more data can be streamed.

What reasons (if any) do the dispatcher and stream give when they end and/or they throw an error?

Not sure if anyone's still working on this, since it was marked closed. I noticed that

// Install discord.js before running this!
const Discord = require('discord.js');
const ytdl    = require('ytdl-core');

const client = new Discord.Client();
client.login('Token here');

client.on('message', message => {
  if (message.content.startsWith('++play')) {
    const voiceChannel = message.member.voiceChannel;
    if (!voiceChannel) {
      return message.reply('Please be in a voice channel first!');
    }
    voiceChannel.join()
      .then(connnection => {
        let stream = ytdl('https://www.youtube.com/watch?v=aatr_2MstrI',{quality:249,filter : 'audioonly'});
        ytdl_debugger(stream,true);
        const dispatcher = connnection.playStream(stream);
        dispatcher.on('end', reason => {
            console.log("reason: "+reason);
          voiceChannel.leave();
        });
      });
  }
});

function ytdl_debugger(stream,debug){
    var size;
    stream.on('response', function(res) {
              size = res.headers['content-length'];
              if(debug) console.log('size', size);
              });

    var dataEmitted = 0;
    stream.on('data', function(chunk) {
              dataEmitted += chunk.length;
              var date = new Date();
              if(debug) console.log(date+': on data', chunk.length, dataEmitted);
              });

    stream.on('end', function() {
              if(debug) console.log('emitted', dataEmitted);
              });
    stream.on('error', function(error) {
              console.log(error);
              });

}

I believe I've isolated it. It dies early if you're getting audio only and when it disconnects. This seems to be because it loads it all at once really quickly. See this log piped from terminal, specifically the time stamps log.txt. It isn't ideal, but a current bypass is to just stream the whole video in a format that isn't wasteful in discord, that seems to work fine. Until (hopefully) a fix?

To anyone also having this problem, you can alternatively hold a buffer of 10 songs or something, well, saving to disk then just playing from file.

From that log:

size 1702823
emitted 1670055

1702823 - 1670055 = 32768

Which is 16384 * 2. It reconnected twice, that must be related.

Two things.

The inconsistent total emitted vs content-length is due to a bad .unpipe() that I just pushed.

The "Stream is not generating quickly enough." end message is from an error in discord.js, both with the latest v11.0.0 and with latest. If that { end: false } is removed, the stream ends without issues. I'm not too familiar with discord.js, so I'm not sure why the transcoder stdin is kept open there.

There's also an issue opened for that already

This seems to still be happening to me, where it's cutting the end of the song off.

@godleydemon Seems fine to me. I've only had some songs fail to load immideatly, but no songs cutting short.

@FireController1847 I'm willing to bet, what it does is times out how long it should be broadcasting, then sets that time by the video length. Then starts playing, but if there is any buffer time at all, that's how much the song will be cut off. It also might be the discord api I'm using that relies on ytdl as I just don't know enough to write my own.

@godleydemon What's the end reason? If the end reason is "The stream is not generating quickly enough", that means it is your internet, not the module.

@FireController1847 I just get playback finished and thats it. I don't get any other output in discord. I also don't get any output in console after the song ends. Unless this person didn't add any debug information into his api.

@godleydemon Discord.js has a Dispatcher.end event. You need to Console.log that event.

@FireController1847 I'm sorry, but this is a really stupid question, would you mind looking at my terrible api I'm utilizing here and telling me where I should put the debug for console.log?
discord.js-music-v11

Some of this is because my head is starting to hurt working on this for almost 10 hours now

@godleydemon It cannot find the page, it gives me a 404.

@FireController1847 sorry about that, that's how bad this is getting I messed up the markup -.- fixed it

@godleydemon Add me on Discord.

FireController1847#9085

@godleydemon if you haven't resolved it yet, try git cloning the master branch and importing that instead, if you're currently using the npm install. I'm not entirely sure if it does it already but it's worth a try if you haven't done so already.

The npm version is up to date with the master branch. Or are you talking about discord.js?

Anyway, is the error you're getting ECONNRESET? Or something else?

Because you mentioned the stream ending right before it ends, it makes me suspect it's the "stream not generating quickly enough" end message that I addressed above.

@fent unfortunately, I still haven't solved it and if I just add in console log event for dispatcher.end it just says end in console. So either I'm not putting something in the right place, or something else. I ended up rewriting everything last night after everything was said and done. That wrapper api I was using was terrible and kept doing funky things. I was able to figure out most of it lol
New Code

So I'm doing it alot different from what I was before. It works and all, but I still get songs getting cut off from time to time.


I did fix this somewhat, by adding a download function, to download the music first to disk and a settimeout function to wait for 7 seconds before playing. So it gives it enough time to search for the video, apply it's variables and then start streaming from file. It now plays the whole file, but I think I'm going to need a more elegant solution as the whole streaming directly from youtube thing still seems to be slightly broken.

On line 37 of that you can add a reason argument to the end event from the dispatcher. Sometimes it'll end with a reason that can help knowing what happened.

I just published a new version with a fix that I hope fixes this once and for all. It fixes the mysterious missing data and ending one buffer event earlier that @ibexes pointed out above, by properly cleaning up the previous request when reconnecting.

A side effect of this error would be that it would mess up the audio decoder in discord.js, since it would skip part of the audio data, making it end the stream after a reconnect.

Please try it and let me know of this fixes this error on your end too.

I think it might have fixed it for me, only time will tell though.

@fent I'm sorry to tell you that, but on the bot I'm currently working on it seems there is some missing data, especially at the end of a video. The bot is (not always, kinda random) missing the last few 5 ~ 10 seconds of a video.

The Discord stream ends with the reason Stream is not generating quickly enough. (which I highly doubt is from a bad connection, since the bot is hosted on a Compute Engine VM with 400mbps down/up).

If you want more info, you can take a look at the repo.

Still happens. If I run locally, it works fine, but when I run it on a server, downloading stops 10-15 seconds before the video ends

Im also having the same issue as username99987, it runs fine on my computer, but running it on Google Cloud Platform, Amazon web services, SkySilk and Heroku all result in the audio cutting out before the song finishes.

@Spellminer this is currently discussed in #402

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NeuronButter picture NeuronButter  路  6Comments

ajgeiss0702 picture ajgeiss0702  路  5Comments

PKPear picture PKPear  路  4Comments

r0hin picture r0hin  路  3Comments

cloudrac3r picture cloudrac3r  路  5Comments