orleans icon indicating copy to clipboard operation
orleans copied to clipboard

ClientConfiguration.LoadFromFile, sync over async

Open danielwertheim opened this issue 8 years ago • 9 comments

Works like a charm when executing in a context where restoring on captured context isn't a problem (e.g. Console) but when switching to e.g. ASP.Net environment where bootstraping happens in a non async flow one might think that the exposed ClientConfiguration.LoadFromFile() is safe (due to being sync). This seems to be wrong.

https://github.com/dotnet/orleans/blob/master/src/Orleans.Core/Configuration/ConfigUtilities.cs#L349 https://github.com/dotnet/orleans/blob/master/src/Orleans.Core/Configuration/ClusterConfiguration.cs#L440

None of the lines has ConfigureAwait(false) so when https://github.com/dotnet/orleans/blob/master/src/Orleans.Core/Configuration/ClientConfiguration.cs#L222 happens, the blocking call seem to cause a dead-lock, at least it hangs for me.

Either expose LoadFromFile as pure async and let the consumer decide or perhaps start using ConfigureAwait(false)? Guess the first option is hard has that would be breaking and exposing LoadFromFileAsync is hard is not an option since that seem to violate the naming convention with other async methods.

danielwertheim avatar Jan 07 '18 09:01 danielwertheim

Hi @danielwertheim

Sorry for the confusion. Our legacy ClientConfiguration and ClusterConfiguration is not great. That's part of the reason why we are going to deprecate them in orleans 2.0. In 2.0, configuration will be mostly replaced by ASP.Net IOption. In other words, each component will have its own POCO class config class, which is compatible with IOption system. As to how to load those config objects, such as from file, env variable etc, ASP.Net provided IConfiguration system to do that. Let me know if the new way is better and whether you have further concern.

xiazen avatar Jan 09 '18 21:01 xiazen

@xiazen Yes, but in the meantime you will risk getting deadlocks (I got them). So if no fix is planned for old version then I guess you should provide documentation on how to configure it in an ASP.Net environment where this can give the deadlock.

danielwertheim avatar Jan 12 '18 17:01 danielwertheim

Can you explain more on why you think there's a deadlock there? I have limited understanding on your application start up flow. Can you tell me more where is the deadlock?

LoadFromFile is a sync method by .NET standard. Are you implying their TaskAWaiter.GetResult() method has a bug in it?

xiazen avatar Jan 12 '18 19:01 xiazen

Some info: https://blog.stephencleary.com/2014/12/a-tour-of-task-part-6-results.html

GetAwaiter().GetResult()

is blocking. And since you do that nested in async flow, you can become victim to deadlocks in ASP.Net as ConfigureAwait is not used.

One way to cause it is to e.g. try and load the config in a controller action:

var c = ClientConfiguration.LoadFromFile(HttpContext.Current.Server.MapPath("~/App_Data/ClientConfiguration.xml"));

The way I bumped into it was during bootstrapping of the application, my dependencies in via Startup that is.

danielwertheim avatar Jan 12 '18 23:01 danielwertheim

I see. Sorry for the inconvenience for you. I can see it can be confusing, and caused you trouble. But you can bypass it through configuring your client in System.Web.HttpApplication.Application_Start, instead of through a controller action, right? Or you can configure it programmatically, instead of using LoadFromFile?

You mentioned that you bumped into it during boostrapping, which is confusing to me. Boostraping is a step in silo side, are you saying you are configuring client in silo boostraping step?

xiazen avatar Jan 16 '18 20:01 xiazen

We can add a documentation warn people about this potential deadlock as well.

xiazen avatar Jan 16 '18 22:01 xiazen

I'm over it as I switched to 2.0 and manual config.

With bootstrap I didn't mean Orleans specific feature, but seen to my app. As I said, in my case it was in Startup.

danielwertheim avatar Jan 17 '18 07:01 danielwertheim

that's good to hear. let me know if you have further issues.

xiazen avatar Jan 17 '18 19:01 xiazen

close this for now, welcome to reopen it if you have more question

xiazen avatar Jan 19 '18 23:01 xiazen