-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-4763): cache resumeToken
in ChangeStream.tryNext()
#4636
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
Merged
+208
−21
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#tryNext()
of the changeStream
249547b
to
9ed6270
Compare
tadjik1
commented
Aug 29, 2025
tadjik1
commented
Aug 29, 2025
#tryNext()
of the changeStream
resumeToken
in ChangeStream.tryNext()
baileympearson
requested changes
Aug 29, 2025
dariakp
requested changes
Sep 2, 2025
93138f0
to
a4f9f9d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment on test title phrasing, otherwise looks good!
dariakp
requested changes
Sep 4, 2025
dariakp
requested changes
Sep 5, 2025
Co-authored-by: Bailey Pearson <[email protected]>
dariakp
approved these changes
Sep 8, 2025
baileympearson
approved these changes
Sep 8, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Change Streams include a feature that allows the stream to resume after certain failures (for example, when a host is unreachable or a cursor is not found). The full list of resumable errors is defined in the spec).
This feature works by having ChangeStream cache the resume token returned by the server on successful operations, and then pass its value in either the
startAfter
orresumeAfter
field when attempting to resume. The server then knows from which point in the oplog to return changes to the client.The caching mechanism is handled internally in both the event-listener form and the iterator form, and it updates the stream’s
resumeToken
property. For example:However, not all exposed methods cache the token. In particular,
.tryNext()
ignores it, so the following code logs the sameresumeToken
for each change:If the stream encounters a resumable error in the snippets above, it will attempt to resume. Without the latest resume token (
.tryNext()
case), the server might return changes that were already consumed, resulting in duplicates on the application side. This issue does not occur with.next()
or with the 'change' event listener, both of which update theresumeToken
correctly.The fix
Handle changes returned by
.tryNext()
the same way as in.next()
by using the private._processChange()
, except when the change isnull
. Because.tryNext()
does not wait for server changes, anull
indicates “no changes,” whereas.next()
would treatnull
as a signal to close the stream.Testing
Resume functionality is covered by a combination of unit and integration tests that verify token caching and the use of
startAfter/resumeAfter
during resume.Additionally, a test case was added for this specific user scenario:
getMore
Release Highlight
ChangeStream
.tryNext()
now updatesresumeToken
to prevent duplicates after resumeWhen
.tryNext()
returns a change document, the driver now caches itsresumeToken
, aligning its behavior with.next()
and the'change'
event. If.tryNext()
returnsnull
(no new changes), nothing is cached, which is unchanged from previous behavior.Previously,
.tryNext()
did not update theresumeToken
, so a resumable error could cause a resume from an older token and re-deliver already processed changes. With this release, resumes continue from the latest token observed via.tryNext()
, preventing duplicates.Applications that poll change streams with
.tryNext()
in non-blocking loops benefit directly. There are no API changes; if you previously tracked and passedresumeAfter
orstartAfter
manually, you can now rely on the driver’s built-in token caching.Huge thanks to @rkistner for bringing this bug to our attention and for sharing code to reproduce it. Huge thanks as well to @Omnicpie for investigating and implementing a fix.
Double check the following
npm run check:lint
scripttype(NODE-xxxx)[!]: description