[CSHARP-2203] SynchronizationContext.Current bleed Created: 27/Feb/18  Updated: 26/Nov/18  Resolved: 26/Nov/18

Status: Closed
Project: C# Driver
Component/s: Write Operations
Affects Version/s: 2.5
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: Steven [X] Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: deadlock, security, threading
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows server


Attachments: Text File AsyncHelper.cs    

 Description   

1. In one independent thread, context, uses a AsyncHelper.RunSync(...) wrapper invoke mongodb's UpdateOneAsync method.
What "AsyncHelper" does is it replaces the SynchronizationContext.Current with a "ExclusiveSynchronizationContext", and replace back, one finished execution.

2. In another completely independent thread/context, executing following code:
code line 1...
... // SynchronizationContext.Current is null
await mongodb.UpdateOneAsync(); //with, or without .ConfigureAwait(false)
...// SynchronizationContext.Current is "ExclusiveSynchronizationContext"

These 2 calls not related to each other.

This is not 100% time happening. But when happen, it causes deadlock, or potential security issue as it leaks SynchronizationContext.

The code of AsyncHelper is attached.



 Comments   
Comment by Robert Stam [ 12/Oct/18 ]

The C# driver does not use use SynchronizationContext and never sets it.

I don't think this issue is C# driver related.

Comment by Steven [X] [ 27/Feb/18 ]

Currently, it only happens when we uses AsyncHelper, as we don't set SynchronizationContext elsewhere.
But suspect it applicable to other SynchronizationContext setting as well.

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