on-internet
← Community

Is a hard timeout for asynchronous operations that don’t work with SemaphoreSlim without creating leakages possible?

Stack Overflow ·

SemaphoreSlim is being used in .NET 8 for restricting concurrent calls to a third-party asynchronous API up to five. The issue here is that this API sometimes gets stuck forever and does not honor CancellationToken. await _semaphore.WaitAsync(ct); try { var work = ThirdPartyClient.ExecuteAsync(itemId, ct); // sometimes ignores ct and hangs if (await Task.WhenAny(work, Task.Delay(5000, ct)) != work) throw new TimeoutException(); await work; } finally { _semaphore.Release(); } Things I've Tried So Far: Releasing in finally (above): Works to keep the pipeline going, but work tasks that hung are s

Comments

Sign in or sign up to comment and upvote.

No comments yet.