zadam / trilium

Build your personal knowledge base with Trilium Notes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

sigaloid opened this issue · comments

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.

commented

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 ...

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)
+        })
     });
 }
commented

@sigaloid ok, let's see if it helps