AspNetCoreDiagnosticScenarios icon indicating copy to clipboard operation
AspNetCoreDiagnosticScenarios copied to clipboard

Recommended handling in ConcurrencyDictionary<string, Task<T>> when task fails

Open passuied opened this issue 5 years ago • 2 comments

Hi @davidfowl,

Trying to get rid of Task.Result in our code and follow your approach in AsyncGuidance.md#concurrentdictionarygetoradd by storing the Task instead of the actual value. In our tests, it causes issue when the task fails (e.g. HTTP Exception) but the task obviously still gets added to the cache...

Any recommendations here?

Thanks, Patrick

passuied avatar Mar 13 '20 16:03 passuied

@passuied Just saw this. This is a tricky one. If you want to remove the entry from the dictionary when it fails then you might need something like GetOrAddAsync (https://gist.github.com/davidfowl/3dac8f7b3d141ae87abf770d5781feed#file-concurrentdictionaryextensions-cs-L53).

davidfowl avatar Mar 21 '21 18:03 davidfowl

Another approach might be to wrap it like this: https://stackoverflow.com/a/33942013/11635 There's an evolution of that with expiration semantics too (there are some tests if one wanted to port it to C#)

bartelink avatar Mar 27 '21 07:03 bartelink