Description
DefaultSleeper.sleepAsync() schedules a TimerTask that completes a CompletableFuture, while close() only cancels the underlying Timer:
override fun close() = timer.cancel()
Timer.cancel() discards scheduled tasks that have not yet run. Their associated futures are not completed or cancelled.
A caller waiting on one of those futures can therefore wait forever after the sleeper is closed. In the SDK this can happen while an async retry is in backoff: RetryingHttpClient composes the next request from sleeper.sleepAsync(backoffDuration), so closing the client during that delay can strand the request future.
Expected behavior
Closing DefaultSleeper should deterministically settle every pending async sleep future rather than abandoning it.
Suggested fix
Track pending timer tasks/futures. On close, cancel the timer and complete or cancel each pending future. Tasks that run normally should remove themselves from the pending set.
The close path should be idempotent, and a post-close sleepAsync() call should return an already-failed/cancelled future rather than throwing synchronously from Timer.schedule.
Regression coverage
- close settles a long pending
sleepAsync() future promptly;
- normally completed sleeps are removed from pending tracking;
- repeated close is safe;
sleepAsync() after close returns a settled exceptional/cancelled future instead of throwing synchronously.
Description
DefaultSleeper.sleepAsync()schedules aTimerTaskthat completes aCompletableFuture, whileclose()only cancels the underlyingTimer:Timer.cancel()discards scheduled tasks that have not yet run. Their associated futures are not completed or cancelled.A caller waiting on one of those futures can therefore wait forever after the sleeper is closed. In the SDK this can happen while an async retry is in backoff:
RetryingHttpClientcomposes the next request fromsleeper.sleepAsync(backoffDuration), so closing the client during that delay can strand the request future.Expected behavior
Closing
DefaultSleepershould deterministically settle every pending async sleep future rather than abandoning it.Suggested fix
Track pending timer tasks/futures. On close, cancel the timer and complete or cancel each pending future. Tasks that run normally should remove themselves from the pending set.
The close path should be idempotent, and a post-close
sleepAsync()call should return an already-failed/cancelled future rather than throwing synchronously fromTimer.schedule.Regression coverage
sleepAsync()future promptly;sleepAsync()after close returns a settled exceptional/cancelled future instead of throwing synchronously.