Skip to content

ShareDB/ws occasionally crashes Node when "readyState 2 (CLOSING)" #275

@houshuang

Description

@houshuang

We expose the ShareDB server within the same Node process that runs our Meteor app (separate websockets from Meteor). It's quite important for us that this main Node process should never crash, since that would negatively impact all connected clients (even if the restart time is quick). Therefore we use try...catch for things that could possibly fail, and deal with them internally.

However, I see a few crashes in the log due to ShareDB's use of Websocket (we use ws on the backend, and reconnecting-websocket on the frontend). I see that ShareDB does check whether the websocket is closed in this snippet

(lib/agent.js:165)

   if (this.closed) return;

  this.backend.emit('send', this, message);
  this.stream.write(message);

But that seems to not be enough, maybe the websocket changes status between line 1 and line 3 above, or maybe this.closed doesn't capture readyState 2?

It seems that attaching a callback function on this.stream.write function also avoids throwing, but I'm not sure if it's safe for ShareDB to just disregard (which it seems to be, since it justs returns if this.closed)?

Error: WebSocket is not open: readyState 2 (CLOSING)
    at WebSocket.send (/built_app/programs/server/npm/node_modules/ws/lib/websocket.js:322:19)
    at WebSocketJSONStream._write (/built_app/programs/server/npm/node_modules/websocket-json-stream/index.js:27:11)
    at doWrite (_stream_writable.js:397:12)
    at writeOrBuffer (_stream_writable.js:383:5)
    at WebSocketJSONStream.Writable.write (_stream_writable.js:290:11)
    at Agent.send (/built_app/programs/server/npm/node_modules/sharedb/lib/agent.js:165:15)
    at Agent._reply (/built_app/programs/server/npm/node_modules/sharedb/lib/agent.js:220:8)
    at callback (/built_app/programs/server/npm/node_modules/sharedb/lib/agent.js:241:15)
    at /built_app/programs/server/npm/node_modules/sharedb/lib/agent.js:497:7
    at /built_app/programs/server/npm/node_modules/sharedb/lib/backend.js:433:9
    at /built_app/programs/server/npm/node_modules/sharedb-mongo/index.js:368:16
    at handleCallback (/built_app/programs/server/npm/node_modules/mongodb/lib/utils.js:120:56)
    at /built_app/programs/server/npm/node_modules/mongodb/lib/cursor.js:683:5
    at handleCallback (/built_app/programs/server/npm/node_modules/mongodb-core/lib/cursor.js:171:5)
    at nextFunction (/built_app/programs/server/npm/node_modules/mongodb-core/lib/cursor.js:691:5)
    at /built_app/programs/server/npm/node_modules/mongodb-core/lib/cursor.js:602:7
    at queryCallback (/built_app/programs/server/npm/node_modules/mongodb-core/lib/cursor.js:232:18)
    at /built_app/programs/server/npm/node_modules/mongodb-core/lib/connection/pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

Versions:
ws 6.1.3
sharedb 1.0.0-beta.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions