You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Nov 8, 2018. It is now read-only.
This is tricky as it may lead user to write buggy code because it's close to impossible to know whether the executing code is thread safe or not. E.g.:
publicclassHomeController:Controller{privatereadonlyMyDbContext_context;publicHomeController(MyDbContextcontext){_context=context;}publicTask<ActionResult>Index(){varparameters=new[]{"cheap","expensive","normal"};foreach(var parameter in parameters){varresults=await _context.Items.FirstOrDefaultAsync(x => x.Category ==parameter);// ...}// ...}}
This code is safe but if we turn this into Task.WhenAll, you will get weird concurrency exceptions as EF's DbContext is not thread safe. So, suggesting this code fix may confuse the developer.
I'd say awaiting in loops is a rather valid scenario like the one above. Awaiting in loops does not have to be a faulty approach to parallelism rather than offloading to background threads. EF is a perfect example for this.
So detecting valid cases for this rule seems super hard to me.
Here, changing to Task.WhenAll(taskArray) is a relatively safe change. It's not truly equivalent, since the looping code will fail immediately when a task it's awaiting on faults, whereas WhenAll will wait for all tasks to complete before returning.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Again, I'm not sure exactly how to spot this, but too much code does something like:
Instead of:
The text was updated successfully, but these errors were encountered: