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

(Bug report) RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear #3374

Open
sigaloid opened this issue Dec 2, 2022 · 3 comments

Comments

@sigaloid
Copy link
Contributor

sigaloid commented Dec 2, 2022

Trilium Version

0.56.2

What operating system are you using?

Other (specify below)

What is your setup?

Local + server sync

Operating System Version

iOS

Description

node:events:505
      throw er; // Unhandled 'error' event
      ^

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear
    at Receiver.getInfo (/usr/src/app/node_modules/ws/lib/receiver.js:176:14)
    at Receiver.startLoop (/usr/src/app/node_modules/ws/lib/receiver.js:136:22)
    at Receiver._write (/usr/src/app/node_modules/ws/lib/receiver.js:83: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 (/usr/src/app/node_modules/ws/lib/websocket.js:1272: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 (/usr/src/app/node_modules/ws/lib/websocket.js:1158: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',
  [Symbol(status-code)]: 1002
}

This error is raised sporadically when iOS + Safari is used. This relates to #3240 - in both cases invalid websockets frames cause the server to go into a broken state that only a restart will fix (returning 502 at all times).

websockets/ws#1354 (comment)

ws conforms to spec and does not look willing to accept invalid frames but maybe the code around this can be made more resilient to this type of error.

@zadam
Copy link
Owner

zadam commented Dec 2, 2022

ws conforms to spec and does not look willing to accept invalid frames but maybe the code around this can be made more resilient to this type of error.

Unfortunately I don't know how. These exceptions are thrown asynchronously and can't be caught by Trilium code ...

@sigaloid
Copy link
Contributor Author

sigaloid commented Dec 7, 2022

Maybe adding an event handler for the websocket server in src/services/ws.js? That's how other users in the thread say they fixed it:
fastify/fastify-websocket#228
peers/peerjs-server#290 (comment)

A diff like this:

diff --git a/src/services/ws.js b/src/services/ws.js
index 4581e894..4d2692a1 100644
--- a/src/services/ws.js
+++ b/src/services/ws.js
@@ -52,6 +52,9 @@ function init(httpServer, sessionParser) {
                 log.error(message);
             }
         });
+        ws.on('error', (error) => {
+            console.log(error)
+        })
     });
 }

zadam added a commit that referenced this issue Dec 7, 2022
@zadam
Copy link
Owner

zadam commented Dec 7, 2022

@sigaloid ok, let's see if it helps

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

No branches or pull requests

2 participants