Skip to content

CSHARP-6080: Pool clear with closeInUseConnections leaks raw ObjectDisposedException to the application instead of a retryable connection error#2042

Merged
sanych-sun merged 4 commits into
mongodb:mainfrom
sanych-sun:CSHARP-6080
Jun 25, 2026
Merged

CSHARP-6080: Pool clear with closeInUseConnections leaks raw ObjectDisposedException to the application instead of a retryable connection error#2042
sanych-sun merged 4 commits into
mongodb:mainfrom
sanych-sun:CSHARP-6080

Conversation

@sanych-sun

Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings June 18, 2026 19:29
@sanych-sun sanych-sun added the bug Fixes issues or unintended behavior. label Jun 18, 2026
@sanych-sun sanych-sun requested a review from a team as a code owner June 18, 2026 19:29
@sanych-sun sanych-sun requested review from BorisDog and ajcvickers and removed request for ajcvickers June 18, 2026 19:29

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 addresses CSHARP-6080 by ensuring that when an ExclusiveConnectionPool is cleared with closeInUseConnections: true, in-flight operations observe a retryable MongoConnectionException instead of a raw ObjectDisposedException.

Changes:

  • Added new tests validating SendMessage/ReceiveMessage throw MongoConnectionException when interrupted by a pool clear that closes in-use connections.
  • Updated ExclusiveConnectionPool’s acquired-connection wrapper to catch ObjectDisposedException from underlying connections and rethrow as MongoConnectionException.

Reviewed changes

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

File Description
tests/MongoDB.Driver.Tests/Core/ConnectionPools/AcquiredConnectionTests.cs Adds regression tests for pool-clear interruption behavior for send/receive.
src/MongoDB.Driver/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs Wraps ObjectDisposedException from acquired connections into MongoConnectionException for retryable error behavior.

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

Comment thread src/MongoDB.Driver/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs Outdated
Comment thread tests/MongoDB.Driver.Tests/Core/ConnectionPools/AcquiredConnectionTests.cs Outdated
Comment thread tests/MongoDB.Driver.Tests/Core/ConnectionPools/AcquiredConnectionTests.cs Outdated
…sposedException to the application instead of a retryable connection error
_connection.Open(operationContext);
SetEffectiveGenerationIfRequired(_connection.Description);
}
catch (ObjectDisposedException ex) when (_closedByPoolClear)

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.

What about Reauthenticate? If it's not needed, add a comment why?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There is no ThrowIfDisposed check in Reauthenticate in BinaryConnection. We can add the same exception handling there too for sameness, but it will be unreachable (at least for now).

@BorisDog BorisDog Jun 24, 2026

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.

I think we should add same ODE handling. Or tests for it. It would be easy to introduce a dispose in the future and miss this update.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment thread src/MongoDB.Driver/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs Outdated
Comment thread src/MongoDB.Driver/Core/ConnectionPools/ExclusiveConnectionPool.Helpers.cs Outdated

@BorisDog BorisDog 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.

LGTM

@sanych-sun sanych-sun merged commit 10c970c into mongodb:main Jun 25, 2026
30 checks passed
@sanych-sun sanych-sun deleted the CSHARP-6080 branch June 25, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes issues or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants