Skip to content

fix: Send pending flag analytics when the client is closed - #259

Open
pcbeingused333 wants to merge 2 commits into
Flagsmith:mainfrom
pcbeingused333:fix/analytics-flush-on-close
Open

pcbeingused333 wants to merge 2 commits into
Flagsmith:mainfrom
pcbeingused333:fix/analytics-flush-on-close

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 29, 2026 •

Copy link
Copy Markdown

FlagsmithClient.close() drops the flag analytics that have not been sent yet.

Cause. AnalyticsProcessor.flush() only runs from trackFeature(), once the 10 second timer has passed. AnalyticsProcessor.close() just shuts down its request processor, so whatever was counted since the last flush is never posted. A short-lived process can lose all of its analytics this way. Events, by contrast, are already sent on close.

Fix. AnalyticsProcessor.close() calls flush() before closing the request processor. The order matters, since the executor rejects new work after shutdown(). flush() returns early when there is nothing buffered. As with the other analytics requests, the post is asynchronous: close() queues it and the executor completes it, without waiting for the response.

flush() and close() are synchronized, so a close() that arrives while another thread is flushing waits for that request to be submitted before shutting the executor down. Previously the executor could be shut down after the buffer had been cleared and before the submit.

Test. testClose_FlushesPendingAnalyticsBeforeShutdown tracks a feature, closes, and checks that the request is submitted before the processor is closed. It fails without the change. testClose_WaitsForActiveFlushBeforeShutdown holds a flush on another thread, calls close(), and checks that the processor is not closed until the request has been submitted.

In one local run of the full suite, the existing timing-based AnalyticsProcessor_checkAnalyticsDataCheckFlushRuns (11 s sleep) failed once; it also failed in a run without this change, and passed in three repeated runs of the class with it.

This change was prepared with the help of an AI coding assistant.

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 29, 2026 00:20
@pcbeingused333
pcbeingused333 requested review from khvn26 and removed request for a team November 29, 2026 00:20

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

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 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files (changes from recent commits).

Confidence score: 4/5

  • AnalyticsProcessorTest can pass even if shutdown happens too early, because the 500 ms join does not confirm closer reached close(). Synchronize on close() before checking the ordering.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java">

<violation number="1" location="src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java:138">
P2: The 500 ms join does not prove `closer` reached `close()`; if it is scheduled only after `finishFlush.countDown()`, an implementation that shuts down immediately can still pass the final ordering check. Wait until the closer is demonstrably blocked on the flush monitor before releasing it, so the test reliably exercises this race.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic


Thread closer = new Thread(analytics::close);
closer.start();
closer.join(500);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The 500 ms join does not prove closer reached close(); if it is scheduled only after finishFlush.countDown(), an implementation that shuts down immediately can still pass the final ordering check. Wait until the closer is demonstrably blocked on the flush monitor before releasing it, so the test reliably exercises this race.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java, line 138:

<comment>The 500 ms join does not prove `closer` reached `close()`; if it is scheduled only after `finishFlush.countDown()`, an implementation that shuts down immediately can still pass the final ordering check. Wait until the closer is demonstrably blocked on the flush monitor before releasing it, so the test reliably exercises this race.</comment>

<file context>
@@ -114,6 +118,35 @@ public void testClose_FlushesPendingAnalyticsBeforeShutdown() {
+
+    Thread closer = new Thread(analytics::close);
+    closer.start();
+    closer.join(500);
+    verify(requestProcessor, never()).close();
+
</file context>
Suggested change
closer.join(500);
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (closer.getState() != Thread.State.BLOCKED && System.nanoTime() < deadline) {
Thread.yield();
}
Assertions.assertEquals(Thread.State.BLOCKED, closer.getState());

@pcbeingused333 pcbeingused333 changed the title fix: send pending flag analytics when the client is closed fix: Send pending flag analytics when the client is closed Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant