Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,19 @@ public void startPollingForDefinitions() {
return t;
});

// Wrap in a Throwable-catching runnable: scheduleAtFixedRate
// permanently cancels future executions if a tick throws, and
// fetchDefinitions only catches Exception — so an Error
// (OOM, StackOverflowError, LinkageError, etc.) would silently
// kill polling for the lifetime of the JVM.
pollingExecutor.scheduleAtFixedRate(
this::fetchDefinitions,
() -> {
try {
fetchDefinitions();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Uncaught throwable in flag-definitions poll; continuing", t);
}
},
config.getPollingIntervalSeconds(),
config.getPollingIntervalSeconds(),
TimeUnit.SECONDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.IOException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static com.mixpanel.mixpanelapi.featureflags.provider.TestUtils.*;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -998,6 +1000,55 @@ public void testUseMostRecentPolledFlagDefinitions() throws Exception {
provider.stopPollingForDefinitions();
}

@Test
public void testPollingSurvivesThrowableFromFetch() throws Exception {
config = LocalFlagsConfig.builder()
.projectToken(TEST_TOKEN)
.enablePolling(true)
.pollingIntervalSeconds(1)
.build();

AtomicInteger callCount = new AtomicInteger(0);
AtomicReference<Throwable> nextThrow = new AtomicReference<>();
String validResponse = buildFlagsResponse(
flagKey, distinctIdContextKey,
Arrays.asList(new Variant("variant", "value", false, 1.0f)),
Arrays.asList(new Rollout(1.0f)),
null
);

LocalFlagsProvider throwingProvider = new LocalFlagsProvider(config, SDK_VERSION, eventSender) {
@Override
protected String httpGet(String urlString) {
callCount.incrementAndGet();
Throwable t = nextThrow.getAndSet(null);
if (t instanceof Error) throw (Error) t;
if (t instanceof RuntimeException) throw (RuntimeException) t;
return validResponse;
}
};

try {
// Initial fetch succeeds (call 1).
throwingProvider.startPollingForDefinitions();
assertEquals(1, callCount.get());

// Arm an Error to escape the next fetch.
nextThrow.set(new Error("simulated JVM-level failure"));
// 1s polling interval → wait long enough for two more ticks.
Thread.sleep(2500);

// Before the fix, scheduleAtFixedRate would have permanently
// cancelled the task on the Error and callCount would stop at 2.
assertTrue(
"Polling should continue after a Throwable: callCount=" + callCount.get(),
callCount.get() >= 3
);
} finally {
throwingProvider.stopPollingForDefinitions();
}
}

// #endregion
// #region getAllVariantsByFlag Tests

Expand Down
Loading