Skip to content

JAVA-6242 Reset BsonDocumentReader to the mark that is reset - #2115

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

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

Conversation

@suvvvv

@suvvvv suvvvv commented Dec 2, 2026

Copy link
Copy Markdown

JAVA-6242

BsonDocumentReader didn't honour the BsonReaderMark.reset() contract when a mark wasn't the only one taken, or was reset more than once:

  • BsonDocumentMarkableIterator.reset() always went back to the start of its buffer, which is the position of the earliest mark taken in that context. So resetting a later mark went back to the earlier one, as described in the ticket.
  • It also stopped buffering after a reset. Values read after that weren't kept, so a second reset, of the same mark or of an earlier one, skipped them.

BsonBinaryReader and JsonReader handle both cases correctly, since their marks are positions in an input they keep.

Fix

  • A Mark now records the iterator position of its context and of each enclosing context, and reset() restores those positions. Before, every iterator in the chain went back to the start of its buffer.
  • Once marked, an iterator keeps buffering the values it returns, so every position stays valid. As a result, the branch of next() that dropped values from the buffer after a reset is gone. The buffer only holds references to values of the document the reader already holds, and it's released with the context when the reader leaves that document or array.
  • Context.mark() and Context.reset() are protected, so I kept them unchanged. Mark now uses new private methods.

Tests

BsonReaderMarkTest (JUnit 5) runs each case against both BsonDocumentReader and BsonBinaryReader, so the two readers are held to the same behaviour:

  • the reproducer from the ticket: a later mark resets to its own position, and the earlier mark is still valid afterwards;
  • the same mark reset twice, reading past the buffered values in between;
  • marks inside nested documents and arrays, reset after the reader has left them, which checks that the enclosing contexts are restored too.

On main, all three fail for BsonDocumentReader and pass for BsonBinaryReader. With the fix, all six pass. I also checked that each part of the change is needed: reverting any one of them makes at least one test fail.

Ran locally with JDK 17: ./gradlew :bson:check (3,905 tests, with the specifications submodule) and the tests of bson-record-codec, bson-kotlin and bson-kotlinx.

BsonDocumentReader reset every mark to the position of the earliest
outstanding mark, and stopped buffering after a reset, so another mark
or the same mark could no longer be reset to. A mark now records the
position of the iterator of its context and of the enclosing contexts,
and a marked iterator keeps the values it returns, as BsonBinaryReader
keeps its whole input.
@suvvvv
suvvvv requested a review from a team as a code owner December 2, 2026 13:16
@suvvvv
suvvvv requested a review from vbabanin December 2, 2026 13:16

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