Skip to content

Commit 80e8aba

Browse files
committed
CSHARP-1628: fixed issue with unobserved task exception.
1 parent cc934b8 commit 80e8aba

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ internal class BinaryConnection : IConnection
5050
private readonly Dropbox _dropbox = new Dropbox();
5151
private DateTime _lastUsedAtUtc;
5252
private DateTime _openedAtUtc;
53+
private Exception _openException;
5354
private readonly object _openLock = new object();
5455
private Task _openTask;
5556
private readonly SemaphoreSlim _receiveLock;
@@ -224,7 +225,7 @@ public void Open(CancellationToken cancellationToken)
224225
}
225226

226227
if (connecting)
227-
{
228+
{
228229
try
229230
{
230231
OpenHelper(cancellationToken);
@@ -233,6 +234,12 @@ public void Open(CancellationToken cancellationToken)
233234
catch (Exception ex)
234235
{
235236
taskCompletionSource.TrySetException(ex);
237+
238+
// this line is here to ensure we treat this exception
239+
// as observed and prevent the TaskScheduler.UnobservedException
240+
// event from getting raised.
241+
_openException = taskCompletionSource.Task.Exception;
242+
236243
throw;
237244
}
238245
}
@@ -740,7 +747,7 @@ public void FailedReceivingMessage(Exception exception)
740747
}
741748

742749
var handler = _connection._failedReceivingMessageEventHandler;
743-
if (handler!= null)
750+
if (handler != null)
744751
{
745752
handler(new ConnectionReceivingMessageFailedEvent(_connection.ConnectionId, _responseTo, exception, EventContext.OperationId));
746753
}

0 commit comments

Comments
 (0)