Repository navigation
fix: Send pending flag analytics when the client is closed - #259
Open
pcbeingused333 wants to merge 2 commits into
Open
pcbeingused333 wants to merge 2 commits into
pcbeingused333 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Confidence score: 4/5
AnalyticsProcessorTestcan pass even if shutdown happens too early, because the 500 ms join does not confirmcloserreachedclose(). Synchronize onclose()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); |
There was a problem hiding this comment.
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()); |
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.
FlagsmithClient.close()drops the flag analytics that have not been sent yet.Cause.
AnalyticsProcessor.flush()only runs fromtrackFeature(), 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()callsflush()before closing the request processor. The order matters, since the executor rejects new work aftershutdown().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()andclose()aresynchronized, so aclose()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_FlushesPendingAnalyticsBeforeShutdowntracks a feature, closes, and checks that the request is submitted before the processor is closed. It fails without the change.testClose_WaitsForActiveFlushBeforeShutdownholds a flush on another thread, callsclose(), 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.