Skip to content

Conversation

rozza
Copy link
Member

@rozza rozza commented Sep 30, 2025

Update SyncMongoClient to assert connections are released

JAVA-5972

Update SyncMongoClient to assert connections are released

JAVA-5972
@rozza rozza requested review from Copilot and nhachicha September 30, 2025 14:27
@rozza rozza requested a review from a team as a code owner September 30, 2025 14:27
Copy link
Contributor

@Copilot Copilot AI left a comment

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 fixes a connection leak issue in the AbstractWriteSearchIndexOperation's executeAsync method and updates the reactive streams test client to properly assert connections are released. The main changes involve modifying SyncMongoClient to track active connections and updating test files to use the updated constructor pattern.

  • Adds connection pooling monitoring to SyncMongoClient to track active connections and assert they are properly released when the client is closed
  • Fixes a variable naming bug in AbstractWriteSearchIndexOperation.executeAsync that was preventing proper connection cleanup
  • Updates all test files to use the new SyncMongoClient constructor pattern that accepts MongoClientSettings directly

Reviewed Changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 2 comments.

File Description
driver-core/src/main/com/mongodb/internal/operation/AbstractWriteSearchIndexOperation.java Fixes variable name bug in executeAsync method
driver-reactive-streams/src/test/functional/com/mongodb/reactivestreams/client/syncadapter/SyncMongoClient.java Adds connection pool monitoring and assertion logic
Multiple test files Updates SyncMongoClient instantiation to use new constructor pattern

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@rozza rozza marked this pull request as draft September 30, 2025 15:41
@rozza
Copy link
Member Author

rozza commented Sep 30, 2025

Looks like there may be more leaks - will review and try to identify the causes.

@rozza rozza marked this pull request as ready for review October 1, 2025 12:18
try {
swallowOrThrow(commandExecutionError);
callback.onResult(result, null);
cb.onResult(result, null);
Copy link
Member Author

Choose a reason for hiding this comment

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

This the reason for the unreleased connection - it used the original and not decorated callback that handles the releasing.

The rest of the PR is updating the test suite to ensure there are no future regressions. The core is the SyncMongoClient close method that now has a listener that counts the connections out and in. It also asserts that all connections are released.

AsyncCallbackSupplier<R> curriedFunction = c -> function.apply(resource, c);
curriedFunction.whenComplete(resource::release).get(errorHandlingCallback);
} catch (Exception e) {
if (resource.getCount() > 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This more closely mirrors the SyncOperationHelper and will ensure any errors thrown outside of the callback also release the resource.

Copy link
Contributor

@nhachicha nhachicha left a comment

Choose a reason for hiding this comment

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

Nice refactor 👍

@rozza rozza merged commit 99b9e43 into mongodb:main Oct 1, 2025
16 of 19 checks passed
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Oct 1, 2025
…ons (mongodb#1812)

Update SyncMongoClient to assert connections are released
Ensure errors thrown outside of the callback also release the resource in AsyncOperationHelper


JAVA-5972
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Oct 6, 2025
…ons (mongodb#1812)

Cherry picked from main to 5.5.x
Ensure errors thrown outside of the callback also release the resource in AsyncOperationHelper

JAVA-5972
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