-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5798: Implement test to track if any UnobservedTaskExceptions were raised while test run #1836
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
CSHARP-5798: Implement test to track if any UnobservedTaskExceptions were raised while test run #1836
Changes from 2 commits
6b9eb50
83e8b52
1c3449e
b8ab963
823a0f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,6 +31,8 @@ namespace MongoDB.Driver.Core.Connections | |||||||||||||||||
| /// </summary> | ||||||||||||||||||
| internal sealed class TcpStreamFactory : IStreamFactory | ||||||||||||||||||
| { | ||||||||||||||||||
| private static readonly byte[] __ensureConnectedBuffer = new byte[1]; | ||||||||||||||||||
|
|
||||||||||||||||||
| // fields | ||||||||||||||||||
| private readonly TcpStreamSettings _settings; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -165,10 +167,18 @@ private void ConfigureConnectedSocket(Socket socket) | |||||||||||||||||
|
|
||||||||||||||||||
| private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancellationToken) | ||||||||||||||||||
| { | ||||||||||||||||||
| var isSocketDisposed = false; | ||||||||||||||||||
| var callbackState = new ConnectOperationState(socket); | ||||||||||||||||||
| using var timeoutCancellationTokenSource = new CancellationTokenSource(_settings.ConnectTimeout); | ||||||||||||||||||
| using var combinedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationTokenSource.Token); | ||||||||||||||||||
| using var cancellationSubscription = combinedCancellationTokenSource.Token.Register(DisposeSocket); | ||||||||||||||||||
| using var cancellationSubscription = combinedCancellationTokenSource.Token.Register(state => | ||||||||||||||||||
| { | ||||||||||||||||||
| var operationState = (ConnectOperationState)state; | ||||||||||||||||||
| if (operationState.IsSucceeded) | ||||||||||||||||||
| { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
| DisposeSocket(operationState.Socket); | ||||||||||||||||||
| }, callbackState); | ||||||||||||||||||
|
Comment on lines
171
to
178
|
||||||||||||||||||
|
|
||||||||||||||||||
| try | ||||||||||||||||||
| { | ||||||||||||||||||
|
|
@@ -185,13 +195,12 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell | |||||||||||||||||
| #else | ||||||||||||||||||
| socket.Connect(endPoint); | ||||||||||||||||||
| #endif | ||||||||||||||||||
| EnsureConnected(socket); | ||||||||||||||||||
| callbackState.IsSucceeded = true; | ||||||||||||||||||
| } | ||||||||||||||||||
| catch | ||||||||||||||||||
| { | ||||||||||||||||||
| if (!isSocketDisposed) | ||||||||||||||||||
| { | ||||||||||||||||||
| DisposeSocket(); | ||||||||||||||||||
| } | ||||||||||||||||||
| DisposeSocket(socket); | ||||||||||||||||||
|
|
||||||||||||||||||
| cancellationToken.ThrowIfCancellationRequested(); | ||||||||||||||||||
| if (timeoutCancellationTokenSource.IsCancellationRequested) | ||||||||||||||||||
|
|
@@ -202,9 +211,8 @@ private void Connect(Socket socket, EndPoint endPoint, CancellationToken cancell | |||||||||||||||||
| throw; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| void DisposeSocket() | ||||||||||||||||||
| static void DisposeSocket(Socket socket) | ||||||||||||||||||
| { | ||||||||||||||||||
| isSocketDisposed = true; | ||||||||||||||||||
| try | ||||||||||||||||||
| { | ||||||||||||||||||
| socket.Dispose(); | ||||||||||||||||||
|
|
@@ -214,6 +222,23 @@ void DisposeSocket() | |||||||||||||||||
| // Ignore any exceptions. | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| static void EnsureConnected(Socket socket) | ||||||||||||||||||
|
||||||||||||||||||
| { | ||||||||||||||||||
| bool originalBlockingState = socket.Blocking; | ||||||||||||||||||
|
||||||||||||||||||
|
|
||||||||||||||||||
| try | ||||||||||||||||||
| { | ||||||||||||||||||
| socket.Blocking = false; | ||||||||||||||||||
| // Try to use the socket to ensure it's connected. On MacOS with net6.0 sometimes Connect is completed successfully even after the socket disposal. | ||||||||||||||||||
| socket.Send(__ensureConnectedBuffer, 0, 0); | ||||||||||||||||||
|
||||||||||||||||||
| // Try to use the socket to ensure it's connected. On MacOS with net6.0 sometimes Connect is completed successfully even after the socket disposal. | |
| socket.Send(__ensureConnectedBuffer, 0, 0); | |
| // Use Poll and Connected to ensure the socket is actually connected. | |
| // This is a cross-platform way to check for a disposed or disconnected socket. | |
| if (!socket.Connected || !socket.Poll(0, SelectMode.SelectWrite)) | |
| { | |
| throw new SocketException((int)SocketError.NotConnected); | |
| } |
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.
Disagree. Sending the 0-bytes is done as per Microsoft's suggestion. Also socket.Connected does not makes any checks, but returns results based on the last operation results. Problem with socket.Poll() - we need to provide a small-timeout, what value should we use?
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.
That's OK. If ensuring the socket is alive takes too long - we want to interrupt the operation by disposing the socket.
Uh oh!
There was an error while loading. Please reload this page.