Skip to content

Skip sleep for non-positive back off intervals - #4897

Merged
sobychacko merged 1 commit into
spring-projects:mainfrom
dongmin132:skip-sleep-for-zero-back-off
Nov 9, 2026
Merged

sobychacko merged 1 commit into
spring-projects:mainfrom
dongmin132:skip-sleep-for-zero-back-off

Conversation

@dongmin132

Copy link
Copy Markdown
Contributor

ListenerUtils.conditionalSleep() and conditionalSleepWithPoll() sleep one slice (SMALL_SLEEP_INTERVAL, 10 ms) before they check the deadline, so an interval of 0 still sleeps about 10 ms. With the default SeekUtils.DEFAULT_BACK_OFF (FixedBackOff(0, 9)), which the reference docs describe as "no back off", DefaultErrorHandler sleeps 10 ms on each of the 9 retries via DefaultBackOffHandler, about 90 ms per failing record on top of the seek and re-poll (measured 109 ms for 9 retries with a running container, 5 ms with container == null). DefaultAfterRollbackProcessor.processBatch() (with commitRecovered and batchRecoverAfterRollback) and the batch retry in ErrorHandlingUtils.retryBatch() pay the same 10 ms per retry with a zero interval.

Cause: both methods are do/while loops whose first statement is Thread.sleep(sleepInterval), so the deadline is only checked after a full slice. unrecoverableBackOff() already guards its stoppableSleep() call with interval > 0, and the container == null branch of DefaultBackOffHandler calls Thread.sleep(0), which does not sleep; the other two entry points had no such check.

Changes:

  • Return immediately from conditionalSleep() and conditionalSleepWithPoll() when interval <= 0, and say so in their Javadoc.

Tests (ListenerUtilsTests, no timing assertions; all three fail on main):

  • conditionalSleepWithNonPositiveInterval: the condition supplier is never evaluated for 0 and -1.
  • conditionalSleepWithPollWithNonPositiveInterval: the supplier is never evaluated and the consumer is never polled.
  • stoppableSleepWithZeroInterval: container.isRunning() is never called.

The full :spring-kafka:test suite passes locally with the change (1278 tests, 4 skipped).

Notes:

  • conditionalSleepWithPoll() already skipped the poll on its first iteration, so for a zero interval it never polled before this change either; consumer liveness in retryBatch() is unchanged.
  • With a zero interval and a running container, retries now run with no pause at all, which is what the container == null branch and the docs already describe. Users who implicitly relied on the 10 ms pacing of FixedBackOff(0, n) batch retries will see a tighter loop.
  • Intervals between 1 and 9 ms still round up to one 10 ms slice; that is left as is.

The zero-interval sleep dates back to GH-1480, which replaced Thread.sleep(nextBackOff) in FailedRecordTracker with stoppableSleep() without the > 0 guard that unrecoverableBackOff() kept, and GH-1845, which lowered the slice from 100 ms to 10 ms for intervals up to 500 ms; GH-3885 copied the loop into conditionalSleepWithPoll(). The FixedBackOff(0L, 2L) samples in annotation-error-handling.adoc ("recover after 3 failures, with no back off") and the SeekUtils.DEFAULT_BACK_OFF Javadoc ("0 interval") already describe the intended behavior, so no docs change is included.

ListenerUtils.conditionalSleep() and conditionalSleepWithPoll() sleep
one slice (SMALL_SLEEP_INTERVAL, 10 ms) before checking the deadline,
so an interval of 0 still sleeps about 10 ms. With the default
SeekUtils.DEFAULT_BACK_OFF, FixedBackOff(0, 9), which the reference
docs describe as "no back off", DefaultErrorHandler slept 10 ms on
each of the 9 retries via DefaultBackOffHandler, about 90 ms per
failing record. DefaultAfterRollbackProcessor.processBatch() and the
batch retry in ErrorHandlingUtils.retryBatch() paid the same 10 ms per
retry with a zero interval.

Return immediately for a non-positive interval, as
unrecoverableBackOff() already does with its interval > 0 check. The
sleep dates back to spring-projectsGH-1480, when stoppableSleep() replaced
Thread.sleep(nextBackOff) in FailedRecordTracker, and spring-projectsGH-1845, which
reduced the slice from 100 ms to 10 ms.

Signed-off-by: Dongmin Kim <m914@naver.com>
@sobychacko sobychacko added this to the 4.2.0 milestone Nov 9, 2026
@sobychacko
sobychacko merged commit 8bfed6a into spring-projects:main Nov 9, 2026
3 checks passed
spring-builds pushed a commit that referenced this pull request Nov 9, 2026
Fixes: #4897

ListenerUtils.conditionalSleep() and conditionalSleepWithPoll() sleep
one slice (SMALL_SLEEP_INTERVAL, 10 ms) before checking the deadline,
so an interval of 0 still sleeps about 10 ms. With the default
SeekUtils.DEFAULT_BACK_OFF, FixedBackOff(0, 9), which the reference
docs describe as "no back off", DefaultErrorHandler slept 10 ms on
each of the 9 retries via DefaultBackOffHandler, about 90 ms per
failing record. DefaultAfterRollbackProcessor.processBatch() and the
batch retry in ErrorHandlingUtils.retryBatch() paid the same 10 ms per
retry with a zero interval.

Return immediately for a non-positive interval, as
unrecoverableBackOff() already does with its interval > 0 check. The
sleep dates back to GH-1480, when stoppableSleep() replaced
Thread.sleep(nextBackOff) in FailedRecordTracker, and GH-1845, which
reduced the slice from 100 ms to 10 ms.

Signed-off-by: Dongmin Kim <m914@naver.com>
(cherry picked from commit 8bfed6a)
spring-builds pushed a commit that referenced this pull request Nov 9, 2026
Fixes: #4897

ListenerUtils.conditionalSleep() and conditionalSleepWithPoll() sleep
one slice (SMALL_SLEEP_INTERVAL, 10 ms) before checking the deadline,
so an interval of 0 still sleeps about 10 ms. With the default
SeekUtils.DEFAULT_BACK_OFF, FixedBackOff(0, 9), which the reference
docs describe as "no back off", DefaultErrorHandler slept 10 ms on
each of the 9 retries via DefaultBackOffHandler, about 90 ms per
failing record. DefaultAfterRollbackProcessor.processBatch() and the
batch retry in ErrorHandlingUtils.retryBatch() paid the same 10 ms per
retry with a zero interval.

Return immediately for a non-positive interval, as
unrecoverableBackOff() already does with its interval > 0 check. The
sleep dates back to GH-1480, when stoppableSleep() replaced
Thread.sleep(nextBackOff) in FailedRecordTracker, and GH-1845, which
reduced the slice from 100 ms to 10 ms.

Signed-off-by: Dongmin Kim <m914@naver.com>
(cherry picked from commit 8bfed6a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants