Skip to content

Commit

Permalink
Check for late addition of listeners before reaping doc
Browse files Browse the repository at this point in the history
In the case where 2 clients are connected to a document, the first
makes changes while the second is disconnected, then disconnects
itself. When the second client connects it will immediately go into
the catchup phase which does not immediately add a listener to the
document - it only starts listening after the catchup is complete.

This can result in a bit of a race condition where the initial check
for listeners will return zero and incorrectly reap the document
by the time the second client has completeted it's catchup.

This avoids reaping the document from underneath clients that are
catching up by adding an additional check immediately before
reaping the document.
  • Loading branch information
nickbrowne committed Feb 23, 2024
1 parent d8e574a commit f70860a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/server/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,16 @@ module.exports = Model = (db, options) ->
clearTimeout doc.reapTimer
doc.reapTimer = reapTimer = setTimeout ->
tryWriteSnapshot docName, ->
# If the reaping timeout has been refreshed while we're writing the snapshot, or if we're
# in the middle of applying an operation, don't reap.
delete docs[docName] if docs[docName].reapTimer is reapTimer and doc.opQueue.busy is false
# If the document has newly added listeners, or the reaping timeout has been refreshed
# while we're writing the snapshot, or if we're in the middle of applying an operation,
# don't reap.
#
# Listeners can appear on the document "late" if they were in the middle of the catchup
# phase when `nextTick` was first called above.
if doc.eventEmitter.listeners('op').length == 0 and
docs[docName].reapTimer is reapTimer and
doc.opQueue.busy is false
delete docs[docName]
, options.reapTime

tryWriteSnapshot = (docName, callback) ->
Expand Down

0 comments on commit f70860a

Please sign in to comment.