Repository navigation
Conversation
…ation first() closed the cursor only when next() terminated. A cancellation while the cursor was being created dropped the cursor without closing it, and a cancellation during next() left it open. Get the cursor the way BatchCursorFlux does, and close it in both cases.
This branch has not been deployed
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
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.
JAVA-6379
BatchCursorPublisher.first()only closed the cursor indoOnTerminate, so a cancelled subscription left the cursor open on the server. There are two cases:first()was one chain from the operation to the result, so the cancellation also cancelled theMono.createthat receives the cursor fromexecuteAsync. The cursor created after that was dropped without being closed. This is the case in the ticket:Mono.from(publisher.first()).subscribe().dispose()never sendskillCursors.next()is waiting for the first batch.doOnTerminatedoesn't run on cancellation.Fix
first()now gets the cursor the wayBatchCursorFluxdoes: it subscribes tobatchCursor(...)separately from the downstream subscription, so the cursor still arrives after a cancellation, and then:next();next()is in progress,onCancelcloses the cursor (CursorResourceManagerdefers the close until thegetMorefinishes);next()terminates, as before.The subscriber context is passed to both inner subscriptions with
contextWrite(sink.contextView()), as inBatchCursorFlux. Closing can happen twice in a race between cancellation and termination, which is fine sinceclose()is idempotent.Tests
BatchCursorFluxTest: the test from the ticket, against a change stream with and withoutfirst(), using its own client and command listener (the class listener ignoreskillCursors). Onmain,first: truetimes out waiting forkillCursors, andfirst: falsepasses.BatchCursorPublisherTest(mocks): one test per case above. The executor emits the cursor afterdispose(), ornext()never completes beforedispose(). Both fail onmain. The second one also checks that the cursor stays open before the cancellation.testBatchCursorFirstnow also checks that the cursor is closed once after a normal result.I also checked that each close path is needed: removing any one of the three makes a test fail.
Ran locally with JDK 17 against a single-node replica set (MongoDB 8.0):
BatchCursorPublisherTest,BatchCursorFluxTest,:driver-reactive-streams:test(3,655 tests; the 22 failures are the same onmain:mongocryptdisn't installed here, and a few tests need more than one node or DNS SRV records), andspotlessApply,checkstyleandspotbugsMainfor the module.