fix: Memory leak in Direct Connection #895
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
onStoreDocument
returns a rejected promise due to failing to acquire a lock.then
to continue to callstoreDocumentHooks
, preventingafterStoreDocument
,unloadDocument
andafterUnloadDocument
to be called. This is how the memory leak occursafterStoreDocument
is never triggered. That's fine, we don't need to release a lock that we never acquired.onDisconnect
hooks when calling .disconnect() on the direct connection, the redis extensionsonDisconnect
logic isn't triggered either. The fix to unload the document associated with the direct connection here https://github.com/ueberdosis/hocuspocus/pull/831/files is never reached.I don't think we should unload the document if the store document hook returns a rejected promise. Take for example, an autosave that is implemented through a debounced
onStoreDocument
. We wouldn't want the server to suddenly unload the active document if for some reason that autosave failed.However, if we confirm that the direct connection is the ONLY connection to that document, we can trigger the 'onDisconnect' hook and can call
unloadDocument
for that document when we call .disconnect() on the direct connection. This should ensure the document is unloaded, consequently preventing unused loaded documents from accumulating on the server.