-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5777: Avoid ThreadPool-dependent IO methods in sync API #1813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Consolidate Stream extension methods.
b4aa35b to
b2bf468
Compare
There was a problem hiding this 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 refactors stream extension methods to consolidate implementations and avoid ThreadPool-dependent I/O methods in the synchronous API. The main changes involve replacing the old API that used OperationContext and TimeSpan parameters with a cleaner API that uses int timeoutMs and CancellationToken parameters directly.
Key Changes:
- Consolidated stream extension methods with separate timeout handling for sync and async operations
- Replaced
TimeSpantimeout parameters withint timeoutMs(milliseconds) for consistency - Improved test coverage by adding dedicated tests for byte array overloads of WriteBytes methods
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/MongoDB.Driver/Core/Misc/StreamExtensionMethods.cs | Complete refactoring of ReadBytes/WriteBytes extension methods with new timeout handling mechanism; sync methods now use Timer-based approach while async methods use Task.WhenAny pattern |
| tests/MongoDB.Driver.Tests/Core/Misc/StreamExtensionMethodsTests.cs | Updated all tests to use new API signatures; added comprehensive test coverage for byte array WriteBytes methods; renamed tests for better clarity |
| src/MongoDB.Driver/GridFS/GridFSBucket.cs | Updated ReadBytes/ReadBytesAsync calls to use new API with named cancellationToken parameter |
| src/MongoDB.Driver/Core/Connections/BinaryConnection.cs | Updated ReadBytes/WriteBytes calls with TimeSpan-to-milliseconds conversion at call sites |
| src/MongoDB.Driver/Core/Connections/Socks5Helper.cs | Updated ReadBytes/ReadBytesAsync calls to use new API with named cancellationToken parameter |
| src/MongoDB.Driver/Core/Compression/SnappyCompressor.cs | Simplified ReadBytes calls by removing unnecessary timeout parameters for in-memory operations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public async Task WriteBytes_with_byte_buffer_should_throw_when_buffer_is_null([Values(true, false)]bool async) | ||
| { | ||
| var stream = new Mock<Stream>().Object; | ||
| byte[] buffer = null; |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test method is named WriteBytes_with_byte_buffer_should_throw_when_buffer_is_null but it's testing the byte array overload instead of the IByteBuffer overload. The variable should be declared as IByteBuffer buffer = null; to properly test the intended method.
| byte[] buffer = null; | |
| IByteBuffer buffer = null; |
| catch (Exception) | ||
| { | ||
| // suppress any exception | ||
| } |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic catch clause.
| catch (Exception) | |
| { | |
| // suppress any exception | |
| } | |
| catch (ObjectDisposedException) | |
| { | |
| // suppress ObjectDisposedException | |
| } | |
| catch (IOException) | |
| { | |
| // suppress IOException | |
| } |
Consolidate Stream extension methods + improve code coverage.
Includes fix for Tailable_cursor_should_be_able_to_be_cancelled_from_a_different_thread_with_expected_result flaky test.