Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent 'afterUnloadDocument' being triggered immediately after onLoadDocument #893

Merged
merged 1 commit into from
Jan 29, 2025

Conversation

bencarletonn
Copy link
Contributor

  • Leading up to the call of the unloadDocument method of the Hocuspocus class, if the document no longer has any clients, the following events are guaranteed to occur in a synchronous nature: onStoreDocument hooks -> afterStoreDocument hooks -> unloadDocument
  • Since the unloadDocument method includes an asynchronous call to trigger all afterUnloadDocument hooks without any await, or without extending the promise chain, the completion of the afterUnloadDocument hook is not guaranteed when the storeDocumentHooks returns a resolved promise.
  • This, in turn, frees the event loop and allows the Hocuspocus server to handle different hooks whilst the logic in the afterUnloadDocument callback is incomplete.

A particular case can arise, prompted by an instant disconnect and reconnect of the websocket, where the afterUnloadDocument hook is triggered immediately after the onLoadDocument hook for a specific document, leading to possible unexpecting behavior.

In our example, we were using the 'afterUnloadDocument' to remove a document from a local cache. You can imagine the problems this has created when that document is removed from the cache despite being active.

@janthurau janthurau merged commit b88ba42 into ueberdosis:main Jan 29, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants