Repository navigation
fix: Clamp an over-large start wait time instead of overflowing - #243
Open
tanderson-ld wants to merge 1 commit into
Open
tanderson-ld wants to merge 1 commit into
tanderson-ld wants to merge 1 commit into
Conversation
LDClient's constructor converted config.startWait to milliseconds with Duration.toMillis(), which throws ArithmeticException for a duration too large to represent in milliseconds (longer than ~292 million years). The conversion ran after dataSystem.start(), so the exception escaped the constructor with the data system already started and no reference available to close it, leaking the client. The start wait is now converted inside the wait block, clamping a value at or beyond the largest representable millisecond duration to Long.MAX_VALUE rather than converting it. A start wait that large already behaved as the maximum representable wait, so this adds no new behavior; it only replaces the overflow-and-leak with that existing bounded maximum. Normal values are unaffected.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LDClient's constructor convertedconfig.startWaitto milliseconds withDuration.toMillis(), which throwsArithmeticExceptionfor a duration too large to represent in milliseconds (longer than ~292 million years — e.g.Duration.ofSeconds(Long.MAX_VALUE)). The conversion ran afterdataSystem.start(), so the exception escaped the constructor with the data system already started (streaming connection, threads, event processor) and no reference available to close it — leaking the client on every construction attempt.Fix
The start wait is converted inside the wait block, clamping a value at or beyond the largest representable millisecond duration (
Duration.ofMillis(Long.MAX_VALUE)) toLong.MAX_VALUErather than converting it:A threshold comparison is used rather than catching the
ArithmeticExceptionso the clamp reads as deliberate; the boundary is definitional (ofMillis(Long.MAX_VALUE)is by definition the largest representable millisecond duration), and computing inside the!isZero() && !isNegative()block means only the positive overflow boundary is ever in play.A start wait that large already behaved as the maximum representable wait at the boundary value, so this adds no new behavior — it only replaces the overflow-and-leak with that existing bounded maximum. Normal values are unaffected;
Duration.ZERO/negative still mean "don't wait".Notes
Clamping to a definite maximum (rather than treating an over-large wait as indefinite) is consistent with the start-wait contract across the server SDK fleet, which model start-wait as a bounded timeout with no "wait indefinitely" mode. (Indefinite initialization waiting exists only in the OpenFeature providers, by OpenFeature-provider-spec requirement, and is handled entirely at the provider layer.)
Found during the OpenFeature Java provider start-wait review. SDK-3418.
Test
LDClientTest.startWaitTooLargeForMillisIsClampedRatherThanOverflowingasserts the clampedLong.MAX_VALUEreachesinitFuture.get(...); reverting the clamp fails it withArithmeticException: long overflow. FullLDClientTest,LDClientOfflineTest,LDClientEndToEndTestand checkstyle pass.Note
Overview
Fixes
LDClientconstructor startup whenLDConfig.startWaitis too large to convert to milliseconds. The code previously calledconfig.startWait.toMillis()afterdataSystem.start(), which can throwArithmeticExceptionand leave the data system running without a usable client reference.LDClientnow comparesstartWaittoDuration.ofMillis(Long.MAX_VALUE)and usesLong.MAX_VALUEwhen the configured wait is at or above that boundary; otherwise it still usestoMillis(). Logging, the excessive-wait warning, andstartFuture.get(...)all use this clampedstartWaitMillis. Zero and negative start waits are unchanged (still skip waiting).Adds
LDClientTest.startWaitTooLargeForMillisIsClampedRatherThanOverflowing, which expectsinitFuture.get(Long.MAX_VALUE, TimeUnit.MILLISECONDS)forDuration.ofSeconds(Long.MAX_VALUE)instead of failing on overflow.Reviewed by Cursor Bugbot for commit 6645f04. Bugbot is set up for automated code reviews on this repo. Configure here.