Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Avoid divide-by-zero in ExponentialBackOff jitter#36932

Closed
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/exponentialbackoff-zero-initial-interval-jitter
Closed

Avoid divide-by-zero in ExponentialBackOff jitter#36932
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/exponentialbackoff-zero-initial-interval-jitter

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

When an ExponentialBackOff is configured with an initialInterval of 0
and a positive jitter, the first nextBackOff() throws
ArithmeticException: / by zero.

Both settings are individually accepted today:

  • setJitter(...) only rejects negative values (Assert.isTrue(jitter >= 0, ...)).
  • There is no validation on initialInterval, and with jitter = 0 an
    initialInterval of 0 already works and yields a delay of 0.

So the combination initialInterval = 0 and jitter > 0 is an accepted
configuration, yet it crashes instead of producing a delay — an inconsistency
between the two jitter branches.

Reproduction

ExponentialBackOff backOff = new ExponentialBackOff();
backOff.setInitialInterval(0);
backOff.setJitter(100);

backOff.start().nextBackOff(); // throws ArithmeticException: / by zero

Root cause

In ExponentialBackOff.ExponentialBackOffExecution#applyJitter:

long applicableJitter = jitter * (interval / initialInterval);

With initialInterval == 0, the first interval is also 0, so this evaluates
jitter * (0 / 0) — an integer division by zero.

Fix

Guard the division so no jitter scaling is applied when initialInterval is 0,
leaving behavior for positive intervals unchanged:

long applicableJitter = jitter * (initialInterval > 0 ? (interval / initialInterval) : 1);

A regression test (jitterWithZeroInitialInterval) is added to
ExponentialBackOffTests.


This is a narrow runtime-crash guard and is orthogonal to the validation design
discussed in #35357 — it stays compatible whichever way that validation is
ultimately implemented.

When an ExponentialBackOff is configured with an initialInterval of 0
and a positive jitter, the first nextBackOff() evaluated
jitter * (interval / initialInterval), performing an integer division
by zero and throwing ArithmeticException.

Both initialInterval = 0 and jitter > 0 are individually accepted
configurations -- with jitter = 0, an initialInterval of 0 already
yields a delay of 0 -- so the combination should not throw. Guard the
division so that no jitter scaling is applied when initialInterval is 0,
leaving behavior for positive intervals unchanged.

Signed-off-by: junhyeong9812 <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 16, 2026
@sbrannen sbrannen self-assigned this Jun 16, 2026
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 16, 2026
@sbrannen sbrannen added this to the 7.0.9 milestone Jun 16, 2026
@sbrannen sbrannen changed the title Avoid divide-by-zero in ExponentialBackOff jitter Avoid divide-by-zero in ExponentialBackOff jitter Jun 17, 2026
sbrannen added a commit that referenced this pull request Jun 17, 2026
@sbrannen sbrannen closed this in 924849f Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants