Skip to content

JAVA-6379 Close the cursor of BatchCursorPublisher.first() on cancellation - #2117

Open
suvvvv wants to merge 1 commit into
mongodb:mainfrom
suvvvv:JAVA-6379
Open

suvvvv wants to merge 1 commit into
mongodb:mainfrom
suvvvv:JAVA-6379

Conversation

@suvvvv

@suvvvv suvvvv commented Dec 4, 2026

Copy link
Copy Markdown

JAVA-6379

BatchCursorPublisher.first() only closed the cursor in doOnTerminate, so a cancelled subscription left the cursor open on the server. There are two cases:

  • The subscription is cancelled while the cursor is being created. first() was one chain from the operation to the result, so the cancellation also cancelled the Mono.create that receives the cursor from executeAsync. The cursor created after that was dropped without being closed. This is the case in the ticket: Mono.from(publisher.first()).subscribe().dispose() never sends killCursors.
  • The subscription is cancelled while next() is waiting for the first batch. doOnTerminate doesn't run on cancellation.

Fix

first() now gets the cursor the way BatchCursorFlux does: it subscribes to batchCursor(...) separately from the downstream subscription, so the cursor still arrives after a cancellation, and then:

  • if the subscription was already cancelled, it closes the cursor without calling next();
  • if the subscription is cancelled while next() is in progress, onCancel closes the cursor (CursorResourceManager defers the close until the getMore finishes);
  • otherwise it closes the cursor when next() terminates, as before.

The subscriber context is passed to both inner subscriptions with contextWrite(sink.contextView()), as in BatchCursorFlux. Closing can happen twice in a race between cancellation and termination, which is fine since close() is idempotent.

Tests

  • BatchCursorFluxTest: the test from the ticket, against a change stream with and without first(), using its own client and command listener (the class listener ignores killCursors). On main, first: true times out waiting for killCursors, and first: false passes.
  • BatchCursorPublisherTest (mocks): one test per case above. The executor emits the cursor after dispose(), or next() never completes before dispose(). Both fail on main. The second one also checks that the cursor stays open before the cancellation. testBatchCursorFirst now 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 on main: mongocryptd isn't installed here, and a few tests need more than one node or DNS SRV records), and spotlessApply, checkstyle and spotbugsMain for the module.

…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.
@suvvvv
suvvvv requested a review from a team as a code owner December 4, 2026 10:29
@suvvvv
suvvvv requested a review from strogiyotec December 4, 2026 10:29

This branch has not been deployed

No deployments
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.

1 participant