Throttler icon indicating copy to clipboard operation
Throttler copied to clipboard

Race condition in `throttle()`

Open MartinMoizard opened this issue 5 months ago • 0 comments

I faced a race condition using func throttle(): it is possible to reach await actor.run(operation) multiple times in less time than the specified duration parameter.

Nothing prevents throttle() to be called simultaneously from different threads. Throttler being an actor, this is safe but the race condition is still here: let lastDate = lastAttemptDate[identifier] can be set from 2 (or more) different threads before self.lastAttemptDate[identifier] = Date() is reached.

When this happens, the operation is called multiple times in a row, breaking the throttle contract.

I'll try to suggest a fix later.

MartinMoizard avatar Jan 29 '24 18:01 MartinMoizard