From 0404b980f990f85eb2951c8b93efdc9147eee89e Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2026 20:16:16 -0400 Subject: [PATCH 1/2] fix: send pending flag analytics when the client is closed --- .../com/flagsmith/threads/AnalyticsProcessor.java | 4 ++++ .../flagsmith/threads/AnalyticsProcessorTest.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java b/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java index da631f56..dcf65d8b 100644 --- a/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java +++ b/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java @@ -161,7 +161,11 @@ private void setNextFlush() { nextFlush = Instant.now().getEpochSecond() + analyticsTimer; } + /** + * Send the analytics data still buffered, then release the request processor. + */ public void close() { + this.flush(); this.requestProcessor.close(); } } diff --git a/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java b/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java index 6895f4b9..47fcaab2 100644 --- a/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java +++ b/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java @@ -3,6 +3,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -23,6 +24,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.InOrder; public class AnalyticsProcessorTest { @@ -100,6 +102,18 @@ public void testClose_ShutsDownExecutor() { verify(requestProcessor, times(1)).close(); } + @Test + public void testClose_FlushesPendingAnalyticsBeforeShutdown() { + analytics.trackFeature("foo"); + + analytics.close(); + + InOrder inOrder = inOrder(requestProcessor); + inOrder.verify(requestProcessor).executeAsync(any(), any()); + inOrder.verify(requestProcessor).close(); + Assertions.assertTrue(analytics.getAnalyticsData().isEmpty()); + } + @Test @SneakyThrows public void AnalyticsProcessor_longAdderGetsSerializedCorrectly() { From 2920882ae98d8cfe43ff90fad04fcdc9a6145e87 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 08:50:18 -0400 Subject: [PATCH 2/2] fix: wait for an active analytics flush before closing close() could shut down the executor while another thread was still inside flush(), after the buffer had been cleared and before the request was submitted. Co-Authored-By: Claude Sonnet 5.5 --- .../flagsmith/threads/AnalyticsProcessor.java | 5 +-- .../threads/AnalyticsProcessorTest.java | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java b/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java index dcf65d8b..6ce42688 100644 --- a/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java +++ b/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java @@ -112,7 +112,7 @@ private HttpUrl getAnalyticsUrl() { /** * Push the analytics to the server. */ - public void flush() { + public synchronized void flush() { // Make sure analytics data is only flushed once. if (isFlushing.compareAndSet(false, true)) { if (analyticsData.isEmpty()) { @@ -163,8 +163,9 @@ private void setNextFlush() { /** * Send the analytics data still buffered, then release the request processor. + * Waits for a flush running on another thread to hand over its request first. */ - public void close() { + public synchronized void close() { this.flush(); this.requestProcessor.close(); } diff --git a/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java b/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java index 47fcaab2..660b26fd 100644 --- a/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java +++ b/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java @@ -5,6 +5,7 @@ import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -17,6 +18,8 @@ import com.flagsmith.config.FlagsmithConfig; import com.flagsmith.config.Retry; import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import lombok.SneakyThrows; import okhttp3.Response; @@ -105,6 +108,7 @@ public void testClose_ShutsDownExecutor() { @Test public void testClose_FlushesPendingAnalyticsBeforeShutdown() { analytics.trackFeature("foo"); + Assertions.assertFalse(analytics.getAnalyticsData().isEmpty()); analytics.close(); @@ -114,6 +118,35 @@ public void testClose_FlushesPendingAnalyticsBeforeShutdown() { Assertions.assertTrue(analytics.getAnalyticsData().isEmpty()); } + @Test + @SneakyThrows + public void testClose_WaitsForActiveFlushBeforeShutdown() { + CountDownLatch flushStarted = new CountDownLatch(1); + CountDownLatch finishFlush = new CountDownLatch(1); + when(api.newPostRequest(any(), any())).thenAnswer(invocation -> { + flushStarted.countDown(); + finishFlush.await(5, TimeUnit.SECONDS); + return null; + }); + analytics.trackFeature("foo"); + Thread flusher = new Thread(analytics::flush); + flusher.start(); + Assertions.assertTrue(flushStarted.await(5, TimeUnit.SECONDS)); + + Thread closer = new Thread(analytics::close); + closer.start(); + closer.join(500); + verify(requestProcessor, never()).close(); + + finishFlush.countDown(); + flusher.join(5000); + closer.join(5000); + + InOrder inOrder = inOrder(requestProcessor); + inOrder.verify(requestProcessor).executeAsync(any(), any()); + inOrder.verify(requestProcessor).close(); + } + @Test @SneakyThrows public void AnalyticsProcessor_longAdderGetsSerializedCorrectly() {