derbyjs / racer

Realtime model synchronization engine for Node.js
1.18k stars 116 forks source link

Be more defensive for delayed doc unload being triggered twice, introduced in a320772a #278

Closed ericyhwang closed 4 years ago

ericyhwang commented 4 years ago

In https://github.com/derbyjs/racer/pull/276, I introduced deferred doc unload to fix a memory leak introduced by a PR last year.

However, if a doc gets two unloads deferred, the second one can fail with a stack trace like this:

     Uncaught TypeError: Cannot read property 'remove' of undefined
      at Doc.unloadDoc (node_modules/racer/lib/Model/subscriptions.js:193:44)
      at Doc._emitNothingPending (node_modules/sharedb/lib/client/doc.js:246:8)
      at Doc._clearInflightOp (node_modules/sharedb/lib/client/doc.js:962:8)
      at Doc._opAcknowledged (node_modules/sharedb/lib/client/doc.js:884:8)
      at Doc._handleOp (node_modules/sharedb/lib/client/doc.js:315:10)
      at Connection.handleMessage (node_modules/sharedb/lib/client/connection.js:246:20)
      at StreamSocket.socket.onmessage (node_modules/sharedb/lib/client/connection.js:139:18)

This fixes the double deferred unload case by being more defensive.