-
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
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
@tadjik1
tadjik1
changed the title
(削除) test(NODE-4763): add tests for resumeToken caching mechanism (削除ここまで)
(追記) fix(NODE-4763): cache resumeToken in Aug 27, 2025
#tryNext()
of the changeStream
(追記ここまで)
@tadjik1
tadjik1
force-pushed
the
NODE-4763
branch
5 times, most recently
from
August 29, 2025 08:51
249547b
to
9ed6270
Compare
tadjik1
tadjik1
commented
Aug 29, 2025
tadjik1
tadjik1
commented
Aug 29, 2025
@tadjik1
tadjik1
changed the title
(削除) fix(NODE-4763): cache resumeToken in
(追記) fix(NODE-4763): cache Aug 29, 2025
#tryNext()
of the changeStream
(削除ここまで)resumeToken
in ChangeStream.tryNext()
(追記ここまで)
@baileympearson
baileympearson
added
the
Primary Review
In Review with primary reviewer, not yet ready for team's eyes
label
Aug 29, 2025
baileympearson
baileympearson
requested changes
Aug 29, 2025
@tadjik1
tadjik1
force-pushed
the
NODE-4763
branch
from
September 1, 2025 10:20
66ae226
to
f35ce18
Compare
dariakp
dariakp
requested changes
Sep 2, 2025
@tadjik1
tadjik1
force-pushed
the
NODE-4763
branch
2 times, most recently
from
September 3, 2025 14:40
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
dariakp
requested changes
Sep 4, 2025
dariakp
dariakp
requested changes
Sep 5, 2025
@dariakp
dariakp
added
Team Review
Needs review from team
and removed
Primary Review
In Review with primary reviewer, not yet ready for team's eyes
labels
Sep 5, 2025
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
@tadjik1
tadjik1
force-pushed
the
NODE-4763
branch
from
September 8, 2025 07:14
2bb43af
to
bfe2fcd
Compare
dariakp
dariakp
approved these changes
Sep 8, 2025
baileympearson
baileympearson
approved these changes
Sep 8, 2025
5 tasks
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.
Uh oh!
There was an error while loading. Please reload this page.
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