yjs / y-websocket

Websocket Connector for Yjs
https://docs.yjs.dev/ecosystem/connection-provider/y-websocket
MIT License
523 stars 261 forks source link

memory leak when persistence not enabled #100

Closed cm226 closed 2 years ago

cm226 commented 2 years ago

Describe the bug I think the web socket server indefinitely holds on to documents even when there are no active connections and persistence is not enabled.

Note that if persistence if enabled then this is not reproducible, which is the reason I think its a bug and not missing documentation.

To Reproduce

  1. start y-websocket server (do not use any persistence)
  2. create a Y.doc in the browser
  3. add any state to the Y.doc
  4. call Y.Doc.destroy() and disconnect the provider
  5. recreate y.doc and reconnect to y-websocket server
  6. The state from the previous document is heald in memory by the server and is reloaded

Expected behavior I think that step 4. should clean-up the memory for that document on the server. This seems like a memory leak to me, without the deleting of old documents long running servers continually consume memory, where different rooms are used.

I think the problem comes from this code in closeConn in /bin/utils.js

if (doc.conns.size === 0 && persistence !== null) {
      // if persisted, we store state and destroy ydocument
      persistence.writeState(doc.name, doc).then(() => {
        doc.destroy()
      })
      docs.delete(doc.name)
    }

the doc is only destroyed in the case where persistence !== null

I think the fix is simple enough. I will prepare PR for it.

Environment Information

Huly®: YJS-518

dmonad commented 2 years ago

Hi @cm226,

Most people will use the in-memory version only for debugging and don't leave it running for too long. For most people, I'd assume the general assumption is that the document continues to live somewhere and is never deleted intentionally.

However, I do understand why you'd want to remove the document. There are legitimate reasons for doing that.

The y-websocket server is a minimalistic implementation of a Yjs backend. I suggest that you fork the server and modify it to your liking.