Mvc: TagHelpers block on Async code

Created on 15 Oct 2018  路  9Comments  路  Source: aspnet/Mvc

Is this a Bug or Feature request?:

Bug

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

Create a new Asp.net Core Web Application project, install https://github.com/benaadams/Ben.BlockingDetector

Add middleware: app.UseBlockingDetection();

Description of the problem:

TagHelpers use async function (ProcessAsync) which then call GetHashForFile synchronously (PartialViewTagHelper, ScriptTagHelper, LinkTagHelper). They should instead open a stream asynchronously and then compute hash.

Maybe related: https://github.com/aspnet/Mvc/issues/6371

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

netcoreapp2.1, AspNetCore.Mvc 2.1.1

investigate

Most helpful comment

@omidtui there are other workarounds. Theoretically, setting asp-append-version="false" should stop all these tags from calculating hash, or just removing asp-append-version="true" since I think it is false by default.

If you want both tag helper and append version, you should also be able to use the cache tag helper to reduce calls to the hash calculation. Caching is heavily dependent on your project's needs, so you'll need to determine the proper way to implement it yourself.

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-2.1

Finally, I would look into seeing if there is something else starving the threads that these TagHelpers need, as opposed to most of the time being spent within these TagHelpers. For me (OP), this was more of a symptom of a deeper issue than a problem in and of itself.

All 9 comments

Thanks for contacting us, @johnrom.
@NTaylorMullen, can you please look into this? Thanks!

The actual hash calculation is synchronous, so I'm not entirely sure creating an async stream helps. Was there an actual issue you observed as a result of the blocking call?

I've been researching sync-on-async areas of a project in an effort to determine a bottleneck, and noticed that this is the source of most of the warnings above in my project. It isn't the bottleneck per-se, but it could have amplified this particular thread-starved scenario.

@johnrom, will you be interested in submitting a PR with the fix?

After digging deeper, it appears @pranavkm would be right and the only way around would be to read the file stream to byte[] and pass that to the ComputeHash function, which would be a big memory trade-off for large files. I'm assuming that ComputeHash eventually reads the stream to a byte[] (or series of byte[]s) in order to calculate the hash, so I don't know how much this actually creates a difference in memory allocation, but I can't tell from the interface.

One post on the matter I found is here: https://www.tabsoverspaces.com/233435-sha1manaded-with-asynchronous-computehash

Closing this as it seems there is not much we can do here.

Hi,

Im getting thread starvation and this seems to be one of the problems. Can this be fixed?

@omidtui the workaround would be to stop using the TagHelpers that run file hash calculations, i.e. Script and Link tagHelper

@omidtui there are other workarounds. Theoretically, setting asp-append-version="false" should stop all these tags from calculating hash, or just removing asp-append-version="true" since I think it is false by default.

If you want both tag helper and append version, you should also be able to use the cache tag helper to reduce calls to the hash calculation. Caching is heavily dependent on your project's needs, so you'll need to determine the proper way to implement it yourself.

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-2.1

Finally, I would look into seeing if there is something else starving the threads that these TagHelpers need, as opposed to most of the time being spent within these TagHelpers. For me (OP), this was more of a symptom of a deeper issue than a problem in and of itself.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xaviergxf picture xaviergxf  路  35Comments

johnnyoshika picture johnnyoshika  路  57Comments

janpieterz picture janpieterz  路  43Comments

angelsix picture angelsix  路  61Comments

dotnetjunkie picture dotnetjunkie  路  43Comments