nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
733 stars 303 forks source link

Fix LeakyBucket cancellation token timeout and memory leak bug #1057

Closed nozzlegear closed 2 months ago

nozzlegear commented 3 months ago

This PR addresses a bug with the LeakyBucket semaphore and request queue used by the LeakyBucketExecutionPolicy. Check #1055 for more details, but to summarize, if the cancellation token is canceled and an OperationCanceledException or TaskCanceledException is thrown while awaiting the semaphore, the request will not be dequeued.

If the semaphore is sufficiently swamped (e.g. by a webhook endpoint or just very active usage), then the wait time between requests will increase continuously until each request is waiting potentially 5, 10, 15 minutes between when it enters the queue and when it actually sends. This can lead to OOM exceptions as requests pile up and eventually crash the application, further compounding the problem if this is a webhook endpoint.