Fix deadlocks in async-to-sync bridge by properly managing synchronization context
Author: qodo-free-for-open-source-projects[bot]Created Mar 17, 2026Updated Apr 20, 2026
Description
- Async method calls through the synchronous RunSync bridge were causing deadlocks due to improper synchronization context management
- Continuations were being queued to the thread pool instead of the custom synchronization context, breaking the synchronous execution model
- The CustomSynchronizationContext implementation needed alignment with the proven Rebus library implementation pattern
Deliverables
- Context Management: Move synchronization context setup/teardown from RunSync into CustomSynchronizationContext.Run() for cleaner separation of concerns
- Continuation Handling: Remove ConfigureAwait(false) from PostCallback to ensure all continuations execute on the custom context
- API Consistency: Add ConfigureAwait(false) to RunSync method to maintain consistent async patterns
- Code Clarity: Add comments explaining the context behavior and why ConfigureAwait is not used in PostCallback
Source: restsharp/RestSharp