peers / peerjs-server

Server for PeerJS
https://peerjs.com
MIT License
4.34k stars 1.08k forks source link

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear #290

Closed tahayk closed 1 year ago

tahayk commented 1 year ago

I have been using PeerJS server for a couple of months, and sometimes I notice that the server crashes because of this error:

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear

Unfortunately I couldn't find a way to reproduce it, any idea how to handle it or fix it ?

Thank you in advance.

jonasgloning commented 1 year ago

Thanks for the report!

I found an issue from the ws library with more information. TLDR: Some clients sometimes send invalid frames. The websocket server is missing an error handler and crashing because of it.

The fix should be just 3 lines of code. I'll publish it in the next few days, probably just after the weekend.

tahayk commented 1 year ago

@jonasgloning thank you very much, looking forward for the next release.

intellix commented 1 year ago

I don't think is completed @tahayk - commits which updated some codestyling were created referencing this ticket and closed. There wasn't anything relating to this ticket yet... I'm also eagerly awaiting it :)

jonasgloning commented 1 year ago

@intellix is right. I tried to create that commit on mobile - but the change didn't get saved, and the commit was mostly empty. I'll try again once I have access to a computer again. Reopening this as a reminder :)

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 1.0.0-rc.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

jonasgloning commented 1 year ago

Hey @tahayk, I found a way to to reproduce your error message:

const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:9000/peerjs?key=peerjs&id=peerjs&key=peerjs');

ws.on('open', () => ws._socket.write(Buffer.from([0x10])));

But the server doesn’t crash. Even very old versions are fine.

Can you tell me a bit more about your environment? I’m afraid without reproducing the crash; we can’t fix it.

RoundDiceChamp commented 1 year ago

Hi @jonasgloning ,

maybe this helps. I'm able to reproduce the error with the following.

SERVER:

const express = require("express");
const { ExpressPeerServer } = require("peer");

const app = express();
app.on("error", (error) => console.error("Express error", error));

const server = app.listen(9000);
server.on("error", (error) => console.error("Server error", error));
server.on("clientError", (error) =>
  console.error("Server client error", error)
);

const peerServer = ExpressPeerServer(server);
peerServer.on("error", (error) =>
  console.error("ExpressPeerServer error", error)
);

app.use("/", peerServer);

CLIENT:

const WebSocket = require("ws");

const ws = new WebSocket("ws://localhost:9000/peerjs?key=peerjs&id=peerjs");

ws.on("error", (error) => console.error("Peer error", error));
ws.on("open", () => ws._socket.write(Buffer.from([0x10])));

The server then crushes with the following: node:events:505 throw er; // Unhandled 'error' event ^

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear at Receiver.getInfo (/Users/nope/repository/PeerTest/node_modules/ws/lib/receiver.js:171:14) at Receiver.startLoop (/Users/nope/repository/PeerTest/node_modules/ws/lib/receiver.js:131:22) at Receiver._write (/Users/nope/repository/PeerTest/node_modules/ws/lib/receiver.js:78:10) at writeOrBuffer (node:internal/streams/writable:389:12) at _write (node:internal/streams/writable:330:10) at Receiver.Writable.write (node:internal/streams/writable:334:10) at Socket.socketOnData (/Users/nope/repository/PeerTest/node_modules/ws/lib/websocket.js:1162:35) at Socket.emit (node:events:527:28) at addChunk (node:internal/streams/readable:315:12) at readableAddChunk (node:internal/streams/readable:289:9) Emitted 'error' event on WebSocket instance at: at Receiver.receiverOnError (/Users/nope/repository/PeerTest/node_modules/ws/lib/websocket.js:1049:13) at Receiver.emit (node:events:527:28) at emitErrorNT (node:internal/streams/destroy:157:8) at emitErrorCloseNT (node:internal/streams/destroy:122:3) at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'WS_ERR_UNEXPECTED_RSV_2_3',

}

tahayk commented 1 year ago

Hey @jonasgloning, I couldn't reproduce the error with your code, I'm running a docker container, everything is here: https://github.com/openreplay/openreplay/tree/main/peers I'm using peerjs-server with expressjs

jonasgloning commented 1 year ago

I could reproduce the crash with the code from @RoundDiceChamp, thanks!

New beta is on npm: https://github.com/peers/peerjs-server/releases/tag/v1.0.0-rc.4

tahayk commented 1 year ago

Thank you very much @jonasgloning I have been using v1.0.0-rc.4 for the past 3 days, so far no issues, except that I'm still getting this issue:

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear
     at Receiver.getInfo (/work/node_modules/ws/lib/receiver.js:171:14)
     at Receiver.startLoop (/work/node_modules/ws/lib/receiver.js:131:22)
     at Receiver._write (/work/node_modules/ws/lib/receiver.js:78:10)
     at writeOrBuffer (node:internal/streams/writable:390:12)
     at _write (node:internal/streams/writable:331:10)
     at Receiver.Writable.write (node:internal/streams/writable:335:10)
     at Socket.socketOnData (/work/node_modules/ws/lib/websocket.js:1162:35)
     at Socket.emit (node:events:527:28)
     at addChunk (node:internal/streams/readable:324:12)
     at readableAddChunk (node:internal/streams/readable:297:9) {
   code: 'WS_ERR_UNEXPECTED_RSV_2_3',
   [Symbol(status-code)]: 1002
 }

but this time I'm able to catch it using the following code: (it wasn't possible before):

const app = express();
const server = app.listen(PORT, HOST, () => {
    console.log(`App listening on http://${HOST}:${PORT}`);
});

const peerServer = ExpressPeerServer(server, {
    debug: true,
    path: '/',
    proxied: true,
    allow_discovery: false
});

const peerError = (error) => {
    console.error('Error type:');
    console.error(error.type);
    console.error('Error message:');
    console.error(error);
}
peerServer.on('error', peerError);
github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 1.0.0-rc.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 1.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 6 months ago

:tada: This issue has been resolved in version 1.1.0-rc.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: