[CSHARP-2564] Cluster calls TrySetResult which might not return for a long time Created: 29/Mar/19  Updated: 28/Oct/23  Resolved: 06/Apr/19

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 2.8.0
Fix Version/s: 2.8.1

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-2573 Use RunContinuationsAsynchronously wi... Backlog

 Description   

UpdateClusterDescription in Cluster.cs calls TrySetResult on a TaskCompletionSource.

There is an issue with how TrySetResult works. If there are already other tasks waiting for this result they will execute synchronously in the current thread which means that TrySetResult might not return for a long time depending on how many tasks start to run as a result. We require that TrySetResult return immediately so that we can continue and finish the process of updating the cluster state.

In .NET Framework 4.6.1 we could use the RunContinuationsAsynchronously TaskCreationOptions to specify that TrySetResult should only schedule waiting tasks for asynchronous execution and not run them synchronously, so that TrySetResult returns as quickly as possible.

Since we currently only require .NET Framework 4.5.2 a work around would be:

Task.Run(() => oldDescriptionChangedTaskCompletionSource.TrySetResult(true));

so that TrySetResult itself is called asynchronously from a new Task.



 Comments   
Comment by Githook User [ 06/Apr/19 ]

Author:

{'email': 'robert@robertstam.org', 'name': 'rstam', 'username': 'rstam'}

Message: CSHARP-2564: Wrap call to TrySetResult in Task.Run.
Branch: v2.8.x
https://github.com/mongodb/mongo-csharp-driver/commit/3a85356d7df7f3f87cbd1db18126ed8e37be3012

Comment by Githook User [ 06/Apr/19 ]

Author:

{'email': 'robert@robertstam.org', 'name': 'rstam', 'username': 'rstam'}

Message: CSHARP-2564: Wrap call to TrySetResult in Task.Run.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/4f8630f9effed03104080f8c1151057d5a195f46

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