Skip to content

DefaultSleeper.close can strand pending sleepAsync futures #889

Description

@sylvesterkaczmarek

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions