[CSHARP-3684] Unobserved task exception when connection times out Created: 21/May/21  Updated: 26/May/21  Resolved: 26/May/21

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.12.0, 2.12.1, 2.12.2, 2.12.3
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Laura Fernandes Nóbrega Neto Assignee: Mikalai Mazurenka (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File UnobservedException.txt    
Issue Links:
Depends
depends on CSHARP-3462 Unobserved task exception on connecti... Closed

 Description   

When an established connection is interrupted, it will cause an unobserved task exception to be thrown, in the latest version of the package.
While mostly harmless, this pollutes logs and might confuse the end users, as such I believe these exceptions should be handled or muted.
This new behavior started on versions 2.12.x

Thrown Exception (Full stack trace attached):

System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> MongoDB.Driver.MongoConnectionException: An exception occurred while receiving a message from the server. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)

 

It can be reproduced with the following .Net Framework Console app:

public static class Program {
public static async Task Main() {
   TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
   var mongoClient = new MongoClient("mongodb+srv://username:password@instance.mongodb.net/Database");
   var database = mongoClient.GetDatabase("Database");
   var collection = database.GetCollection<object>("Collection");
   var count = await collection.CountDocumentsAsync(f => true);
   Console.WriteLine($"Document count: {count}");
   
   Console.WriteLine("Stop server or disconnect from the internet now");
   Debugger.Break();
   
   try {
      var count2 = await collection.CountDocumentsAsync(f => true);
      Console.WriteLine($"Document count: {count2}");
   } catch {
      // Do nothing, this is expected to fail
   }
   Console.ReadLine();
}
 
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) {
   // This should never be called, but it is
   Console.WriteLine($"UnobservedTaskException: {sender.GetType()}\n{e.Exception}");
}
}



 Comments   
Comment by Mikalai Mazurenka (Inactive) [ 26/May/21 ]

Hi laura.neto@deptagency.com,

Thanks for reporting this issue!

It was recently fixed in CSHARP-3462 and should be in the next driver minor release, so I'm closing this as duplicate with the reference to the ticket above.
 

Generated at Wed Feb 07 21:45:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.