Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server crashes with Invalid WebSocket frame: RSV1 must be clear #1777

Closed
1 task done
aantthony opened this issue Jul 20, 2020 · 8 comments
Closed
1 task done

Server crashes with Invalid WebSocket frame: RSV1 must be clear #1777

aantthony opened this issue Jul 20, 2020 · 8 comments

Comments

@aantthony
Copy link

  • I've searched for any related issues and avoided creating a duplicate
    issue.

Description

The example code no longer works. The server will throw RangeError: Invalid WebSocket frame: RSV1 must be clear and exit.

Reproducible in:

  • version: 7.3.1
  • Node.js version(s): v12.18.2
  • OS version(s): Mac OS 10.15.5 (19F101)

Steps to reproduce:

npm install ws
node -e """
const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});
"""
npx wscat -c ws://localhost:8080/ -x "Hello"

Expected result:

It should log the message

Actual result:

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

RangeError: Invalid WebSocket frame: RSV1 must be clear
    at Receiver.getInfo (/Users/anthony/src/tmp/node_modules/ws/lib/receiver.js:178:14)
    at Receiver.startLoop (/Users/anthony/src/tmp/node_modules/ws/lib/receiver.js:131:22)
    at Receiver._write (/Users/anthony/src/tmp/node_modules/ws/lib/receiver.js:78:10)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Receiver.Writable.write (_stream_writable.js:318:11)
    at Socket.socketOnData (/Users/anthony/src/tmp/node_modules/ws/lib/websocket.js:872:35)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:271:9)
Emitted 'error' event on WebSocket instance at:
    at Receiver.receiverOnError (/Users/anthony/src/tmp/node_modules/ws/lib/websocket.js:777:13)
    at Receiver.emit (events.js:315:20)
    at errorOrDestroy (internal/streams/destroy.js:108:12)
    at onwriteError (_stream_writable.js:418:5)
    at onwrite (_stream_writable.js:445:5)
    at Receiver.startLoop (/Users/anthony/src/tmp/node_modules/ws/lib/receiver.js:152:5)
    at Receiver._write (/Users/anthony/src/tmp/node_modules/ws/lib/receiver.js:78:10)
    [... lines matching original stack trace ...]
    at Receiver.Writable.write (_stream_writable.js:318:11) {
  [Symbol(status-code)]: 1002
}
@lpinca
Copy link
Member

lpinca commented Jul 20, 2020

I can't reproduce, what version of wscat are you using? Also you need a listener for the 'error' event on the server.

wss.on('connection', function connection(ws) {
  ws.on('error', console.error);

  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

@aantthony
Copy link
Author

I tried adding the error handler, it logged the same error. I then tried making a complete example (https://github.com/aantthony/ws-example). Doing a git pull, npm install && node index.js, then npm test resulted in the error.

I restarted my Mac, and now it works.

@lpinca
Copy link
Member

lpinca commented Jul 20, 2020

I restarted my Mac, and now it works.

Ok, I'm closing then.

@lpinca lpinca closed this as completed Jul 20, 2020
@pimvanderheijden
Copy link

I have a crashing application due to this. It appears to ignore the error handler on the WS instance and simply throws. Is that possible?

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

RangeError: Invalid WebSocket frame: invalid status code 1006
    at Receiver.controlMessage (/app/node_modules/ws/lib/receiver.js:464:18)
    at Receiver.getData (/app/node_modules/ws/lib/receiver.js:350:42)
    at Receiver.startLoop (/app/node_modules/ws/lib/receiver.js:143:22)
    at Receiver._write (/app/node_modules/ws/lib/receiver.js:78:10)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Receiver.Writable.write (_stream_writable.js:318:11)
    at Socket.socketOnData (/app/node_modules/ws/lib/websocket.js:900:35)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)

Emitted 'error' event on WebSocket instance at:
    at Receiver.receiverOnError (/app/node_modules/ws/lib/websocket.js:805:13)
    at Receiver.emit (events.js:314:20)
    at errorOrDestroy (internal/streams/destroy.js:108:12)
    at onwriteError (_stream_writable.js:418:5)
    at onwrite (_stream_writable.js:445:5)
    at Receiver.startLoop (/app/node_modules/ws/lib/receiver.js:152:5)
    at Receiver._write (/app/node_modules/ws/lib/receiver.js:78:10)
    [... lines matching original stack trace ...]
 {
  [Symbol(status-code)]: 1002
}

@pimvanderheijden
Copy link

pimvanderheijden commented Dec 3, 2020

I've been reading the code at Receiver.controlMessage and Receiver.receiverOnError but can't seem to find a reason for it to throw an Error besides emitting it.

@ericrange
Copy link

seriously guys, our saas went down by this problem. why do you crash the entire app if there is no error handler? why??

sometimes you define a workflow and dont care about errors because those errors are not critical. why do you crash the entire app????

@sdrsdr
Copy link

sdrsdr commented Nov 29, 2021

@ericrange Can you provide an example code that crashes for 8.x series? BTW in nodejs unhandled error events are thrown by the node libraries internally. Just put a line with .on("error",()=>{}) and move on :) If your case is more complicated do elaborate

https://nodejs.org/api/events.html#error-events

@pimvanderheijden
Copy link

Isn't it a common pattern to simply throw instead of emit in case there is no handler? Thrown errors can be caught after all...

The original issue was more around a unexpected throw even when there is an even emitter attached.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants