Skip to content

Commit 754ee94

Browse files
authored
fix: Clamp an over-large start wait time instead of overflowing (#243)
## Summary `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 — e.g. `Duration.ofSeconds(Long.MAX_VALUE)`). The conversion ran **after** `dataSystem.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)`) to `Long.MAX_VALUE` rather than converting it: ```java long startWaitMillis = config.startWait.compareTo(MAX_MILLIS_START_WAIT) >= 0 ? Long.MAX_VALUE : config.startWait.toMillis(); ``` A threshold comparison is used rather than catching the `ArithmeticException` so 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.startWaitTooLargeForMillisIsClampedRatherThanOverflowing` asserts the clamped `Long.MAX_VALUE` reaches `initFuture.get(...)`; reverting the clamp fails it with `ArithmeticException: long overflow`. Full `LDClientTest`, `LDClientOfflineTest`, `LDClientEndToEndTest` and checkstyle pass. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > Fixes **`LDClient` constructor startup** when `LDConfig.startWait` is too large to convert to milliseconds. The code previously called `config.startWait.toMillis()` after `dataSystem.start()`, which can throw **`ArithmeticException`** and leave the data system running without a usable client reference. > > **`LDClient`** now compares `startWait` to `Duration.ofMillis(Long.MAX_VALUE)` and uses **`Long.MAX_VALUE`** when the configured wait is at or above that boundary; otherwise it still uses `toMillis()`. Logging, the excessive-wait warning, and `startFuture.get(...)` all use this clamped **`startWaitMillis`**. Zero and negative start waits are unchanged (still skip waiting). > > Adds **`LDClientTest.startWaitTooLargeForMillisIsClampedRatherThanOverflowing`**, which expects `initFuture.get(Long.MAX_VALUE, TimeUnit.MILLISECONDS)` for `Duration.ofSeconds(Long.MAX_VALUE)` instead of failing on overflow. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 6645f04. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 4d40f5c commit 754ee94

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/LDClient.java‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.UnsupportedEncodingException;
3232
import java.security.InvalidKeyException;
3333
import java.security.NoSuchAlgorithmException;
34+
import java.time.Duration;
3435
import java.util.ArrayList;
3536
import java.util.Collections;
3637
import java.util.List;
@@ -72,6 +73,10 @@ public final class LDClient implements LDClientInterface {
7273

7374
private static final int EXCESSIVE_INIT_WAIT_MILLIS = 60000;
7475

76+
// The largest start wait the SDK can represent in milliseconds. A longer wait is clamped to it rather than
77+
// converted, because converting it would overflow and throw (and would do so after the data system starts).
78+
private static final Duration MAX_MILLIS_START_WAIT = Duration.ofMillis(Long.MAX_VALUE);
79+
7580
/**
7681
* Creates a new client instance that connects to LaunchDarkly with the default configuration.
7782
* <p>
@@ -259,15 +264,21 @@ public LDClient(String sdkKey, LDConfig config) {
259264
// Start the data system
260265
Future<Void> startFuture = dataSystem.start();
261266
if (!config.startWait.isZero() && !config.startWait.isNegative()) {
267+
// Convert the start wait to milliseconds, clamping a wait too large to represent to the maximum rather
268+
// than converting it, which would overflow and throw. Only the positive boundary matters here, since
269+
// zero and negative waits are excluded above.
270+
long startWaitMillis = config.startWait.compareTo(MAX_MILLIS_START_WAIT) >= 0
271+
? Long.MAX_VALUE
272+
: config.startWait.toMillis();
262273
if (!dataSystem.isInitialized()) {
263274
baseLogger.info("Waiting up to {} milliseconds for LaunchDarkly client to start...",
264-
config.startWait.toMillis());
265-
if (config.startWait.toMillis() > EXCESSIVE_INIT_WAIT_MILLIS) {
266-
baseLogger.warn("LaunchDarkly client created with start wait time of {} milliseconds. We recommend a timeout of less than {} milliseconds.", config.startWait.toMillis(), EXCESSIVE_INIT_WAIT_MILLIS);
275+
startWaitMillis);
276+
if (startWaitMillis > EXCESSIVE_INIT_WAIT_MILLIS) {
277+
baseLogger.warn("LaunchDarkly client created with start wait time of {} milliseconds. We recommend a timeout of less than {} milliseconds.", startWaitMillis, EXCESSIVE_INIT_WAIT_MILLIS);
267278
}
268279
}
269280
try {
270-
startFuture.get(config.startWait.toMillis(), TimeUnit.MILLISECONDS);
281+
startFuture.get(startWaitMillis, TimeUnit.MILLISECONDS);
271282
} catch (TimeoutException e) {
272283
baseLogger.error("Timeout encountered waiting for LaunchDarkly client initialization");
273284
} catch (Exception e) {

‎lib/sdk/server/src/test/java/com/launchdarkly/sdk/server/LDClientTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,25 @@ public void willWaitForDataSourceIfWaitMillisIsGreaterThanZero() throws Exceptio
331331
mocks.verifyAll();
332332
}
333333

334+
@Test
335+
public void startWaitTooLargeForMillisIsClampedRatherThanOverflowing() throws Exception {
336+
// A start wait time too large to represent in milliseconds must be clamped, not converted directly:
337+
// converting it throws ArithmeticException, and it would do so after the data source has started, leaving
338+
// the data source running with no reference available to close it.
339+
LDConfig.Builder config = new LDConfig.Builder()
340+
.startWait(Duration.ofSeconds(Long.MAX_VALUE));
341+
342+
expect(dataSource.start()).andReturn(initFuture);
343+
expect(initFuture.get(Long.MAX_VALUE, TimeUnit.MILLISECONDS)).andReturn(null);
344+
expect(dataSource.isInitialized()).andReturn(false).anyTimes();
345+
mocks.replayAll();
346+
347+
client = createMockClient(config);
348+
assertFalse(client.isInitialized());
349+
350+
mocks.verifyAll();
351+
}
352+
334353
@Test
335354
public void noWaitForDataSourceIfWaitMillisIsNegative() throws Exception {
336355
LDConfig.Builder config = new LDConfig.Builder()

0 commit comments

Comments
 (0)