multithreading - Calling Autofacs Container.DisposeAsync* runs *DisposeDisposeAsync* methods of registered components on arbitra

In Disposer.cs L80 (this one normally completes synchronously hence it has no impact) and L90 (this one

In Disposer.cs L80 (this one normally completes synchronously hence it has no impact) and L90 (this one impacts the situation below) you are using .ConfigureAwait(false) which runs the continuations on the thread pool and not on the SynchronizationContext.

This leads to the behavior, that all subsequent Dispose and DisposeAsync calls after the first DisposeAsync (which runs on the SynchronizationContext) run on worker threads and not on the thread which called Container.DisposeAsync.

In the synchronous case, Container.Dispose will call all Dispose methods on the same thread where it was invoked.

The unexpected thread change during disposal leads to the problem that thread-sensitive Dispose methods (e.g. accessing UI elements running on a UI thread) get into trouble and must take care of threading by their own.

I could not figure out from the docs what the intended threading model is and what to expect/assume, when calling Container.DisposeAsync.

Getting rid of .ConfigureAwait(false) in L80 and L90 would ensure that each Dispose and DisposeAsync call runs on the respective SynchronizationContext on which Container.DisposeAsync is called.

Is this the intended behavior by AutoFac? If so, why is the .ConfigureAwait(false) necessary? If it is necessary, how can I deal with these 'thread-changing' situation during dispose.

Thanks for any insights for this issue.

In Disposer.cs L80 (this one normally completes synchronously hence it has no impact) and L90 (this one impacts the situation below) you are using .ConfigureAwait(false) which runs the continuations on the thread pool and not on the SynchronizationContext.

This leads to the behavior, that all subsequent Dispose and DisposeAsync calls after the first DisposeAsync (which runs on the SynchronizationContext) run on worker threads and not on the thread which called Container.DisposeAsync.

In the synchronous case, Container.Dispose will call all Dispose methods on the same thread where it was invoked.

The unexpected thread change during disposal leads to the problem that thread-sensitive Dispose methods (e.g. accessing UI elements running on a UI thread) get into trouble and must take care of threading by their own.

I could not figure out from the docs what the intended threading model is and what to expect/assume, when calling Container.DisposeAsync.

Getting rid of .ConfigureAwait(false) in L80 and L90 would ensure that each Dispose and DisposeAsync call runs on the respective SynchronizationContext on which Container.DisposeAsync is called.

Is this the intended behavior by AutoFac? If so, why is the .ConfigureAwait(false) necessary? If it is necessary, how can I deal with these 'thread-changing' situation during dispose.

Thanks for any insights for this issue.

Share Improve this question asked Mar 13 at 12:53 Bernd StoetzelBernd Stoetzel 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Looking at the Microsoft.Extensions.DependencyInjection disposal mechanism, it appears they, too, do ConfigureAwait(false) for async disposables not using ValueTask. That's admittedly a miss in Autofac, but it's consistent.

While I think Autofac could improve with some support for ValueTask, the consistent behavior here is valuable to ensure folks switching from one container to another won't be surprised.

We'd be happy to take a PR to make Autofac ValueTask and async disposable handling even closer to the Microsoft container, but to ensure consistent behavior, we likely won't switch how ConfigureAwait works.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744699634a4588702.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信