Skip to content

Improvements for connection desynchronization and hardening of error handling with connections#1998

Draft
rozza wants to merge 4 commits into
mongodb:mainfrom
rozza:JAVA-6210
Draft

Improvements for connection desynchronization and hardening of error handling with connections#1998
rozza wants to merge 4 commits into
mongodb:mainfrom
rozza:JAVA-6210

Conversation

@rozza

@rozza rozza commented Jun 10, 2026

Copy link
Copy Markdown
Member

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR (JAVA-6120) hardens InternalStreamConnection against connection desynchronization and improves robustness when underlying stream operations fail, ensuring problematic connections are closed and command monitoring emits the correct failure events.

Changes:

  • Close connections on desynchronization conditions (e.g., MongoInternalException such as responseTo mismatch) while keeping connections reusable for fully-read command error responses.
  • Ensure async header/body parsing failures close the connection and release buffers before closing the underlying stream.
  • Add unit tests covering async/sync failure modes (write/read errors, header/body parsing failures, responseTo mismatches, command monitoring event correctness).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java Closes desynced connections, broadens exception catching to include Throwable for stream I/O, and guarantees buffer release before closing in async receive paths.
driver-core/src/test/unit/com/mongodb/internal/connection/InternalStreamConnectionTest.java Adds targeted unit tests for desync/error-handling behavior and command monitoring event emission.
.agents/references/testing-guide.md Documents additional testing conventions relevant to these new tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rozza rozza force-pushed the JAVA-6210 branch 2 times, most recently from f80da3f to 83eb39a Compare June 11, 2026 10:51
@rozza rozza requested a review from Copilot June 11, 2026 10:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

rozza added 3 commits June 11, 2026 12:18
A responseTo mismatch indicates the stream is desynchronized, so the
connection must be closed to prevent it returning to the pool. Command
error responses (ok: 0) leave the stream intact and do not close the
connection.

Command monitoring now receives exactly one started and one terminal
event in all mismatch scenarios, on both sync and async paths: the
commandSuccessful flag is set only after the succeeded event was
actually emitted, and the async path sends the failed event for any
failure when no succeeded event went out, matching the sync path.

Response buffers are released before the connection is closed:
NettyStream.close() requires all buffers it handed out to have been
released already.

JAVA-6210
Ensure all error paths close the connection to prevent pooled
connections from retaining unread response data:
- sendMessage (sync): catch Throwable instead of Exception
- sendMessageAsync: catch Throwable instead of Exception
- readAsync: catch Throwable instead of Exception
- MessageHeaderCallback: close on header parsing failure
- MessageCallback: close on body read failure and body parsing failure
- MessageCallback: deliver the parsed response outside the try block so
  a throwing downstream callback cannot close a healthy connection

Header and body buffers are released before the connection is closed:
NettyStream.close() requires all buffers it handed out to have been
released already.

Errors are translated to MongoInternalException, consistent with the
pre-existing receiveResponseBuffers behavior.

JAVA-6210
Capture conventions surfaced during review: import types rather than
inline fully-qualified names, prefer atomics over array wrappers for
state captured by lambdas, assert the most specific exception type,
and pair side-effect tests with their negative counterparts.

JAVA-6210

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

When reading an OP_COMPRESSED response, the uncompressed buffer
allocated via getBuffer(...) leaked if uncompress, flip, or ReplyHeader
parsing threw before ResponseBuffers took ownership, on both the sync
(receiveResponseBuffers) and async (MessageCallback) paths. Release the
buffer explicitly on those failure paths.

JAVA-6210
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.

2 participants