-
Notifications
You must be signed in to change notification settings - Fork 574
Context agnostic async methods continuation #5892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@11v1 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
cf0833a to
6a72225
Compare
|
Using Instead of the (at the time) risk of missing adding ConfigureAwait which could result in a regression breaking someone, WCF Client went a different path. WCF will always go async for any channel proxy call. Even if the outgoing request is able to be sent synchronously (e.g. the OS buffers the outgoing payload on the socket), we are going to be asynchronously waiting for the reply anyway. There's no point in trying to avoid an unnecessary thread hop as we're going to have that happen anyway at some point. What we do instead is at the earliest opportunity, we forcibly jump threads to a worked thread on the threadpool. We drop the execution context so we don't flow the SynchronizationContext. This means we don't need to ever call ConfigureAwait once we're past this point as it will always be a no-op due to already being on the default worker threadpool. If you are having a problem which you think is caused by WCF not using ConfigureAwait, it's almost certainly not caused by that. If your problem goes away after applying these changes, it's more likely there's a race condition in your code that the extra overhead is changing the timing of and you are now avoiding. We have pretty robust tests around this. The only way you could have a problem is if you are using the WCF channel stack without a channel proxy (raw Message calls using a channel created from a raw ChannelFactory created directly from Binding.CreateChannelFactory). If this is your scenario, let me know and I can help. Basically, this isn't a problem, can you describe what symptoms you are seeing that you are trying to fix? |
|
@mconnew, thank you for the detailed answer. Your assumption about our usage of WCF without a channel proxy is correct - we create the channel directly via ChannelFactory. Our investigation showed that Adding The reason I opted to add ConfigureAwait(false) broadly is that it’s a safe and defensive approach. It avoids the need to reason about whether a method is a public API / WCF entry point, or an internal method that is only ever called after the execution context has already been dropped. That distinction requires fairly deep knowledge of the WCF execution model, and it’s easy for a specific call path to be overlooked - which may be what happened here. You may be right that our usage is incorrect; however, a few observations still stand:
|
|
StackTrace is before the previous one: If Possibly, this particular execution path is missing a switch to the worker thread. |
|
The call stack is going to help, I should be and to work out a fix. |
|
I think the issue is I only implemented this for request/reply channel shapes, and it didn't get added to the IOutputChannel shaped channels, which we got away with due to SocketAwaitableEventArgs.SendAsync completing synchronously. Can you try adding |
|
By the way, as a temporary workaround, you can do this: var result = await Task.Run(() => channel.DoSomethingAsync());This would effectively do the same thing we're doing deeper into our code. |
Apparently, the existing implementation does not account for applications with a synchronization context. Most methods lack ConfigureAwait calls, which causes continuations to capture the current synchronization context and resume execution on a context-specific thread.
This behavior is important for UI applications, where continuations may resume on the UI thread and, in certain scenarios (especially sync-over-async), can lead to deadlocks. For general-purpose library code, it is recommended to use ConfigureAwait(false) to avoid context capture and remain environment-agnostic.
This PR changes: