Androz2091 / discord-player

🎧 Complete framework to simplify the implementation of music commands using discord.js v14
https://discord-player.js.org/
MIT License
606 stars 192 forks source link

Skip throws Error [ERR_STREAM_PREMATURE_CLOSE] #1570

Closed Cyopn closed 1 year ago

Cyopn commented 1 year ago

It happens when i use skip and stop, throws the exception, but the queue doesnt ends, rather it restarts but in another order. The error picks them up from the event Player.on("error").

Full exception: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close at new NodeError (node:internal/errors:387:5) at PCMResampler.onclose (node:internal/streams/end-of-stream:140:30) at PCMResampler.emit (node:events:525:35) at emitCloseNT (node:internal/streams/destroy:138:10) at processTicksAndRejections (node:internal/process/task_queues:82:21) { code: 'ERR_STREAM_PREMATURE_CLOSE' } discord-player: dev

Thanks.

twlite commented 1 year ago

You can ignore this error, it is caused by the usage of pipeline. I will fix this

Victorcassiano commented 1 year ago

when I use the command the music stops and after a few seconds the bot disconnects. Another error is when, sometimes, I use .addTrack() sometimes the song jumps to the second one that was added

nerblock commented 1 year ago

I'm getting this error too using the latest version from npm:

[2023-03-25 13:10:01] [ERROR] Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:393:5)
    at Encoder.<anonymous> (node:internal/streams/pipeline:352:14)
    at Encoder.emit (node:events:525:35)
    at Encoder.emit (node:domain:489:12)
    at emitCloseNT (node:internal/streams/destroy:132:10)
    at processTicksAndRejections (node:internal/process/task_queues:81:21) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}

The solution is to ignore uncaught exceptions but then pm2 can't restart the process when something else breaks. Can I ask when will the fix be included in npm?

twlite commented 1 year ago

This fix is already included in discord-player part (handled on dsp filters pipeline). You might be getting this error elsewhere because if it was from discord-player, the error event would have caught it.

Edit: as per your stacktrace, it seems to be coming from Encoder class, which does not exist in discord-player

nerblock commented 1 year ago

My trace is similar to the one in the first comment. Only difference I can see is the PCMResampler class, which is replaced with Encoder in my trace. The exception occurs when I queue.delete() or player.destroy() while playing a youtube video. Do you think my problem is unrelated to this bug?

twlite commented 1 year ago

My trace is similar to the one in the first comment. Only difference I can see is the PCMResampler class, which is replaced with Encoder in my trace. The exception occurs when I queue.delete() or player.destroy() while playing a youtube video. Do you think my problem is unrelated to this bug?

The original error of this issue came from https://github.com/Androz2091/discord-player/blob/ee275af0758372e3f2a3b45b2d96bcbf1eb2f166/packages/equalizer/src/FiltersChainBuilder.ts#L55 which is handled in current version.

It could be due to a fault in npm install causing some dependencies to break, you can try removing node_modules and your lockfile, executing npm cache clean --force and finally running npm install to see if that is the case.

nerblock commented 1 year ago

Unfortunately the problem persists after removing node_modules and cleaning the cache. I've found Encoder in prism-media:

function loadOpus(refresh = false) {
  if (Opus.Encoder && !refresh) return Opus;

  Opus = loader.require([
    ['@discordjs/opus', opus => ({ Encoder: opus.OpusEncoder })],
    ['node-opus', opus => ({ Encoder: opus.OpusEncoder })],
    ['opusscript', opus => ({ Encoder: opus })],
  ]);
  return Opus;
}
nerblock commented 1 year ago

The original error of this issue came from

https://github.com/Androz2091/discord-player/blob/ee275af0758372e3f2a3b45b2d96bcbf1eb2f166/packages/equalizer/src/FiltersChainBuilder.ts#L55

I added console logging to this file and can confirm the exception comes from some other place.

nerblock commented 1 year ago

Well, I gave up and just added filtering to the uncaught exception handler. Not pretty but works.

process.on("uncaughtException", function (err) {
  if (!err.stack?.includes("ERR_STREAM_PREMATURE_CLOSE")) {
    console.error(err.stack);
    exitHandler.bind(null, { exit: true, signal: "uncaughtException" });
  }
});