discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Two different WebSocket errors and FFMPEG error when playing audio file to Channel #4134

Closed KM0KA closed 3 years ago

KM0KA commented 4 years ago

Please describe the problem you are having in as much detail as possible:

Hello all. Seeking your help on a difficult issue.

I keep getting two WebSocket errors and one FFMPEG error when playing audio to a channel. They don't occur at the same time for obvious reasons. One fires and throws an exception and the app crashes.

I have an audio queue that loops. The sequence is:

Reload the queue. Join a channel Play an existing file Playback ends. Leave the channel Repeat.

I can run this for days without error and then randomly check and see it has blown up.

I run an auto loop routine on Six Different Channels. Each Channel runs in its own Bot Space, controlling one and only one Voice Channel. One Bot may crash, and the others are not affected. I typically only get one Bot to crash during any testing cycle. Lengths of audio varies, so Channel Join and Leave for all six are random.

I have tried using try {} catch (error) {} so that the app does not crash and I can't seem to catch these two errors.

I am concerned that I am getting these errors to begin with. So what would be a solution for that?

And then how can I catch these thrown errors so deep under the code layers. On the code layer my Bots run, I can can throw an error, catch it, and simply restart from the beginning via the recursion that controls the loop.

The issue is fired with the connection.play call.

I am using a Promise so I can restart when the "finish" event fires for the Dispatcher:

this.dispatcher.on('start', dispatcher => {});

Here is the first WebSocket error:

Error [ERR_UNHANDLED_ERROR]: Unhandled error. (ErrorEvent { target: WebSocket { _events: [Object: null prototype] { open: [Function], message: [Function], close: [Function], error: [Function] }, _eventsCount: 4, _maxListeners: undefined, readyState: 2, protocol: '', _binaryType: 'nodebuffer', _closeFrameReceived: false, _closeFrameSent: false, _closeMessage: '', _closeTimer: null, _closeCode: 1006, _extensions: {}, _receiver: null, _sender: null, _socket: null, _bufferedAmount: 0, _isServer: false, _redirects: 0, url: 'wss://us-west67.discord.media/?v=4&encoding=json', _req: null,

}, type: 'error', message: 'getaddrinfo ENOTFOUND us-west67.discord.media', error: Error: getaddrinfo ENOTFOUND us-west67.discord.media at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26) { errno: -3008, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'us-west67.discord.media' } }) at VoiceConnection.emit (events.js:305:17) at VoiceConnection.EventEmitter.emit (domain.js:485:12) at VoiceWebSocket. (VoiceConnection.js:423:32) at VoiceWebSocket.emit (events.js:316:20) at VoiceWebSocket.EventEmitter.emit (domain.js:485:12) at VoiceWebSocket.onError (VoiceWebSocket.js:160:10) at WebSocket.onError (event-target.js:128:16) at WebSocket.emit (events.js:316:20) at WebSocket.EventEmitter.emit (domain.js:485:12) at ClientRequest. (websocket.js:555:15) { code: 'ERR_UNHANDLED_ERROR', context: ErrorEvent { target: WebSocket { _events: [Object: null prototype], _eventsCount: 4, _maxListeners: undefined, readyState: 2, protocol: '', _binaryType: 'nodebuffer', _closeFrameReceived: false, _closeFrameSent: false, _closeMessage: '', _closeTimer: null, _closeCode: 1006, _extensions: {}, _receiver: null, _sender: null, _socket: null, _bufferedAmount: 0, _isServer: false, _redirects: 0, url: 'wss://us-west67.discord.media/?v=4&encoding=json', _req: null,

},
type: 'error',
message: 'getaddrinfo ENOTFOUND us-west67.discord.media',
error: Error: getaddrinfo ENOTFOUND us-west67.discord.media
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'us-west67.discord.media'
}

} }

//****

Here is the second WebSocket error:

Error [ERR_UNHANDLED_ERROR]: Unhandled error. (ErrorEvent { target: WebSocket { _events: [Object: null prototype] { open: [Function], message: [Function], close: [Function], error: [Function] }, _eventsCount: 4, _maxListeners: undefined, readyState: 2, protocol: '', _binaryType: 'nodebuffer', _closeFrameReceived: false, _closeFrameSent: false, _closeMessage: '', _closeTimer: null, _closeCode: 1006, _extensions: {}, _receiver: null, _sender: null, _socket: null, _bufferedAmount: 0, _isServer: false, _redirects: 0, url: 'wss://us-west321.discord.media/?v=4&encoding=json', _req: ClientRequest { _events: [Object: null prototype], _eventsCount: 5, _maxListeners: undefined, outputData: [], outputSize: 0, writable: true, _last: true, chunkedEncoding: false, shouldKeepAlive: false, useChunkedEncodingByDefault: false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, _contentLength: 0, _hasBody: true, _trailer: '', finished: true, _headerSent: true, socket: [TLSSocket], _header: 'GET /?v=4&encoding=json HTTP/1.1\r\n' + 'Sec-WebSocket-Version: 13\r\n' + 'Sec-WebSocket-Key: fVMt5Vxa9ehDgLvq/lqyNA==\r\n' + 'Connection: Upgrade\r\n' + 'Upgrade: websocket\r\n' + 'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n' + 'Host: us-west321.discord.media\r\n' + '\r\n', _onPendingData: [Function: noopPendingOutput], agent: undefined, socketPath: undefined, method: 'GET', maxHeaderSize: undefined, insecureHTTPParser: undefined, path: '/?v=4&encoding=json', _ended: false, res: [IncomingMessage], aborted: true, timeoutCb: null, upgradeOrConnect: false, parser: [HTTPParser], maxHeadersCount: null, reusedSocket: false,

  [Symbol(kNeedDrain)]: false,
  [Symbol(corked)]: 0,
  [Symbol(kOutHeaders)]: [Object: null prototype]
},
[Symbol(kCapture)]: false

}, type: 'error', message: 'Unexpected server response: 409', error: Error: Unexpected server response: 409 at ClientRequest. (websocket.js:580:7) at ClientRequest.emit (events.js:316:20) at ClientRequest.EventEmitter.emit (domain.js:485:12) at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:610:27) at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17) at HTTPParser.execute () at TLSSocket.socketOnData (_http_client.js:479:22) at TLSSocket.emit (events.js:316:20) at TLSSocket.EventEmitter.emit (domain.js:485:12) at addChunk (_stream_readable.js:297:12) }) at VoiceConnection.emit (events.js:305:17) at VoiceConnection.EventEmitter.emit (domain.js:485:12) at VoiceWebSocket. (VoiceConnection.js:423:32) at VoiceWebSocket.emit (events.js:316:20) at VoiceWebSocket.EventEmitter.emit (domain.js:485:12) at VoiceWebSocket.onError (VoiceWebSocket.js:160:10) at WebSocket.onError (event-target.js:128:16) at WebSocket.emit (events.js:316:20) at WebSocket.EventEmitter.emit (domain.js:485:12) at abortHandshake (websocket.js:698:15) { code: 'ERR_UNHANDLED_ERROR', context: ErrorEvent { target: WebSocket { _events: [Object: null prototype], _eventsCount: 4, _maxListeners: undefined, readyState: 2, protocol: '', _binaryType: 'nodebuffer', _closeFrameReceived: false, _closeFrameSent: false, _closeMessage: '', _closeTimer: null, _closeCode: 1006, _extensions: {}, _receiver: null, _sender: null, _socket: null, _bufferedAmount: 0, _isServer: false, _redirects: 0, url: 'wss://us-west321.discord.media/?v=4&encoding=json', _req: [ClientRequest],

},
type: 'error',
message: 'Unexpected server response: 409',
error: Error: Unexpected server response: 409
    at ClientRequest.<anonymous> (websocket.js:580:7)
    at ClientRequest.emit (events.js:316:20)
    at ClientRequest.EventEmitter.emit (domain.js:485:12)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:610:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17)
    at HTTPParser.execute (<anonymous>)
    at TLSSocket.socketOnData (_http_client.js:479:22)
    at TLSSocket.emit (events.js:316:20)
    at TLSSocket.EventEmitter.emit (domain.js:485:12)
    at addChunk (_stream_readable.js:297:12)

} }

//****

Here is the FFMPEG related error:

Dispatcher Play Error: TypeError: Cannot read property '_readableState' of undefined TypeError: Cannot read property '_readableState' of undefined at new FFmpeg (node_modules/prism-media/src/core/FFmpeg.js:54:40) at AudioPlayer.playUnknown (BasePlayer.js:47:20) at VoiceConnection.play (PlayInterface.js:71:28)

events.js:293 throw er; // Unhandled 'error' event ^

Error: spawn node_modules/ffmpeg-static/ffmpeg EMFILE at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19) at onErrorNT (internal/child_process.js:467:16) at processTicksAndRejections (internal/process/task_queues.js:84:21) Emitted 'error' event on ChildProcess instance at: at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12) at onErrorNT (internal/child_process.js:467:16) at processTicksAndRejections (internal/process/task_queues.js:84:21) { errno: -24, code: 'EMFILE', syscall: 'spawn node_modules/ffmpeg-static/ffmpeg', path: 'node_modules/ffmpeg-static/ffmpeg', spawnargs: [ '-i', '-', '-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2', 'pipe:1' ] }

Include a reproducible code sample here, if possible:

// Place your code here

return new Promise((resolve, reject) => {

try {

  this.dispatcher = connection.play(fileStream, { highWaterMark: 100, bitrate: 128000, volume: 1, plp: 0, fec: true} ); 

} catch (error) { 

    reject(error);

}

}):

Further details:

amishshah commented 4 years ago

Can you fill in the "Further details" part of the issue template.

KM0KA commented 4 years ago

Completed "Further details" section above.

Add new WebSocket error after running without error since initial post:

Play . . .

events.js:307 throw err; // Unhandled 'error' event ^ Error [ERR_UNHANDLED_ERROR]: Unhandled error. (ErrorEvent { target: WebSocket { _events: [Object: null prototype] { open: [Function], message: [Function], close: [Function], error: [Function] }, _eventsCount: 4, _maxListeners: undefined, readyState: 2, protocol: '', _binaryType: 'nodebuffer', _closeFrameReceived: false, _closeFrameSent: false, _closeMessage: '', _closeTimer: null, _closeCode: 1006, _extensions: {}, _receiver: null, _sender: null, _socket: null, _bufferedAmount: 0, _isServer: false, _redirects: 0, url: 'wss://us-west321.discord.media/?v=4&encoding=json', _req: ClientRequest { _events: [Object: null prototype], _eventsCount: 4, _maxListeners: undefined, outputData: [], outputSize: 0, writable: true, _last: true, chunkedEncoding: false, shouldKeepAlive: true, useChunkedEncodingByDefault: false, sendDate: false, _removedConnection: false, _removedContLen: false, _removedTE: false, _contentLength: 0, _hasBody: true, _trailer: '', finished: true, _headerSent: true, socket: [TLSSocket], _header: 'GET /?v=4&encoding=json HTTP/1.1\r\n' + 'Sec-WebSocket-Version: 13\r\n' + 'Sec-WebSocket-Key: wo1+HpU8KtJFtZ3O7A723g==\r\n' + 'Connection: Upgrade\r\n' + 'Upgrade: websocket\r\n' + 'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n' + 'Host: us-west321.discord.media\r\n' + '\r\n', _onPendingData: [Function: noopPendingOutput], agent: undefined, socketPath: undefined, method: 'GET', maxHeaderSize: undefined, insecureHTTPParser: undefined, path: '/?v=4&encoding=json', _ended: false, res: null, aborted: true, timeoutCb: null, upgradeOrConnect: false, parser: [HTTPParser], maxHeadersCount: null, reusedSocket: false,

  [Symbol(kNeedDrain)]: false,
  [Symbol(corked)]: 0,
  [Symbol(kOutHeaders)]: [Object: null prototype]
},
[Symbol(kCapture)]: false

}, type: 'error', message: 'WebSocket was closed before the connection was established', error: Error: WebSocket was closed before the connection was established at WebSocket.close (node_modules/ws/lib/websocket.js:220:14) at VoiceWebSocket.reset (node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:53:60) at VoiceWebSocket.shutdown (node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:44:10) at VoiceConnection.connect (node_modules/discord.js/src/client/voice/VoiceConnection.js:412:42) at VoiceConnection.reconnect (node_modules/discord.js/src/client/voice/VoiceConnection.js:344:10) at VoiceConnection.setTokenAndEndpoint (node_modules/discord.js/src/client/voice/VoiceConnection.js:234:12) at ClientVoiceManager.onVoiceServer (node_modules/discord.js/src/client/voice/ClientVoiceManager.js:47:32) at Object.module.exports [as VOICE_SERVER_UPDATE] (node_modules/discord.js/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js:5:16) at WebSocketManager.handlePacket (node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31) at WebSocketShard.onPacket (node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22) }) at VoiceConnection.emit (events.js:305:17) at VoiceConnection.EventEmitter.emit (domain.js:485:12) at VoiceWebSocket. (node_modules/discord.js/src/client/voice/VoiceConnection.js:422:32) at VoiceWebSocket.emit (events.js:316:20) at VoiceWebSocket.EventEmitter.emit (domain.js:485:12) at VoiceWebSocket.onError (node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:160:10) at WebSocket.onError (node_modules/ws/lib/event-target.js:128:16) at WebSocket.emit (events.js:316:20) at WebSocket.EventEmitter.emit (domain.js:485:12) at abortHandshake (node_modules/ws/lib/websocket.js:697:15) { code: 'ERR_UNHANDLED_ERROR', context: ErrorEvent { target: WebSocket { _events: [Object: null prototype], _eventsCount: 4, _maxListeners: undefined, readyState: 2, protocol: '', _binaryType: 'nodebuffer', _closeFrameReceived: false, _closeFrameSent: false, _closeMessage: '', _closeTimer: null, _closeCode: 1006, _extensions: {}, _receiver: null, _sender: null, _socket: null, _bufferedAmount: 0, _isServer: false, _redirects: 0, url: 'wss://us-west321.discord.media/?v=4&encoding=json', _req: [ClientRequest],

},
type: 'error',
message: 'WebSocket was closed before the connection was established',
error: Error: WebSocket was closed before the connection was established
    at WebSocket.close (node_modules/ws/lib/websocket.js:220:14)
    at VoiceWebSocket.reset (node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:53:60)
    at VoiceWebSocket.shutdown (node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:44:10)
    at VoiceConnection.connect (node_modules/discord.js/src/client/voice/VoiceConnection.js:412:42)
    at VoiceConnection.reconnect (node_modules/discord.js/src/client/voice/VoiceConnection.js:344:10)
    at VoiceConnection.setTokenAndEndpoint (node_modules/discord.js/src/client/voice/VoiceConnection.js:234:12)
    at ClientVoiceManager.onVoiceServer (node_modules/discord.js/src/client/voice/ClientVoiceManager.js:47:32)
    at Object.module.exports [as VOICE_SERVER_UPDATE] (node_modules/discord.js/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js:5:16)
    at WebSocketManager.handlePacket node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (node_modules/discord.js/src/client/websocket/WebSocketShard.js:436:22)

} }

KM0KA commented 4 years ago

Hi. Is there any update on these issues?

Thanks.

skick1234 commented 3 years ago

I'm also dealing with this problem in the latest version (12.4.1)

{
  target: WebSocket {
    _events: [Object: null prototype] {
      open: [Function],
      message: [Function],
      close: [Function],
      error: [Function]
    },
    _eventsCount: 4,
    _maxListeners: undefined,
    readyState: 2,
    protocol: '',
    _binaryType: 'nodebuffer',
    _closeFrameReceived: false,
    _closeFrameSent: false,
    _closeMessage: '',
    _closeTimer: null,
    _closeCode: 1006,
    _extensions: {},
    _receiver: null,
    _sender: null,
    _socket: null,
    _bufferedAmount: 0,
    _isServer: false,
    _redirects: 0,
    url: 'wss://sydney4015.discord.media/?v=4&encoding=etf',
    _req: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      connection: [TLSSocket],
      _header: 'GET /?v=4&encoding=etf HTTP/1.1\r\n' +
        'Sec-WebSocket-Version: 13\r\n' +
        'Sec-WebSocket-Key: x/VNTik2jJGMnV7fgo8taw==\r\n' +
        'Connection: Upgrade\r\n' +
        'Upgrade: websocket\r\n' +
        'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n' +
        'Host: sydney4015.discord.media\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: undefined,
      socketPath: undefined,
      method: 'GET',
      insecureHTTPParser: undefined,
      path: '/?v=4&encoding=etf',
      _ended: false,
      res: null,
      aborted: true,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: [HTTPParser],
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'sydney4015.discord.media',
      protocol: 'https:',
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    [Symbol(kCapture)]: false
  },
  type: 'error',
  message: 'WebSocket was closed before the connection was established',
  error: Error: WebSocket was closed before the connection was established
      at WebSocket.close (/root/DisTube/node_modules/ws/lib/websocket.js:217:14)
      at VoiceWebSocket.reset (/root/DisTube/node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:53:60)
      at VoiceWebSocket.shutdown (/root/DisTube/node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:44:10)
      at VoiceConnection.cleanup (/root/DisTube/node_modules/discord.js/src/client/voice/VoiceConnection.js:392:10)
      at VoiceConnection._disconnect (/root/DisTube/node_modules/discord.js/src/client/voice/VoiceConnection.js:367:10)
      at ClientVoiceManager.onVoiceStateUpdate (/root/DisTube/node_modules/discord.js/src/client/voice/ClientVoiceManager.js:55:18)
      at VoiceStateUpdate.handle (/root/DisTube/node_modules/discord.js/src/client/actions/VoiceStateUpdate.js:31:22)
      at Object.module.exports [as VOICE_STATE_UPDATE] (/root/DisTube/node_modules/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js:4:35)
      at WebSocketManager.handlePacket (/root/DisTube/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
      at WebSocketShard.onPacket (/root/DisTube/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
      at WebSocketShard.onMessage (/root/DisTube/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
      at WebSocket.onMessage (/root/DisTube/node_modules/ws/lib/event-target.js:125:16)
      at WebSocket.emit (events.js:314:20)
      at Receiver.receiverOnMessage (/root/DisTube/node_modules/ws/lib/websocket.js:797:20)
      at Receiver.emit (events.js:314:20)
      at Receiver.dataMessage (/root/DisTube/node_modules/ws/lib/receiver.js:428:14)
}