Skip to content

Conversation

@neelkanth-kaushik
Copy link
Contributor

@neelkanth-kaushik neelkanth-kaushik commented Nov 17, 2025

This pull request improves the shutdown process and reliability of the AnalyticsClient by ensuring that background tasks are properly completed or cancelled during shutdown, and by handling potential race conditions and errors more robustly. The main changes focus on ensuring that the message processing looper and network executor are gracefully terminated, and that any failures during shutdown are properly logged and handled.

Improvements to shutdown and task management:

  • Added a Future<?> looperFuture field to track the background looper task, and updated initialization to store the submitted looper's future. (AnalyticsClient.java)
  • Introduced a waitForLooperCompletion() method to wait for the looper to finish processing all messages before shutting down executors, preventing race conditions where batches could be lost. (AnalyticsClient.java)
  • Enhanced the shutdownAndWait method to handle forceful shutdowns, log detailed information, and specifically address the looper executor's shutdown process, including handling interrupts and reporting on tasks that fail to terminate. (AnalyticsClient.java)

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request enhances the shutdown reliability of the AnalyticsClient by preventing race conditions and ensuring proper cleanup of background tasks. The changes introduce explicit tracking of the looper thread, add a wait mechanism to ensure message processing completes before shutdown, and handle cases where batch submission fails during shutdown.

Key Changes

  • Added looperFuture tracking to wait for looper completion before shutting down executors (lines 72, 136-138, 241-257)
  • Enhanced shutdownAndWait() with forceful shutdown logic specifically for the looper executor (lines 259-307)
  • Added RejectedExecutionException handling to properly notify callbacks when batch submission fails during shutdown (lines 366-381)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 241 to 257
private void waitForLooperCompletion() {
if (looperFuture != null) {
try {
// Wait for the looper to complete processing the STOP message and finish
// Use a reasonable timeout to avoid hanging indefinitely
looperFuture.get(5, TimeUnit.SECONDS);
log.print(VERBOSE, "Looper completed successfully.");
} catch (Exception e) {
log.print(ERROR, e, "Error waiting for looper to complete: %s", e.getMessage());
// Cancel the looper if it's taking too long or if there's an error
if (!looperFuture.isDone()) {
looperFuture.cancel(true);
log.print(VERBOSE, "Looper was cancelled due to timeout or error.");
}
}
}
}
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

The new waitForLooperCompletion() method lacks test coverage. Consider adding tests to verify:

  1. Successful looper completion within timeout
  2. Looper timeout and cancellation behavior
  3. Exception handling during looper wait
  4. Behavior when looperFuture is null

This is important for ensuring the shutdown race condition fix works as intended.

Copilot uses AI. Check for mistakes.
Comment on lines +366 to +381
try {
networkExecutor.submit(
BatchUploadTask.create(AnalyticsClient.this, batch, maximumRetries));
} catch (RejectedExecutionException e) {
log.print(
ERROR,
e,
"Failed to submit batch %s to network executor during shutdown. Batch will be lost.",
batch.sequence());
// Notify callbacks about the failure
for (Message msg : batch.batch()) {
for (Callback callback : callbacks) {
callback.failure(msg, e);
}
}
}
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

The new RejectedExecutionException handling lacks test coverage. Consider adding a test that verifies:

  1. The error is properly logged when batch submission fails during shutdown
  2. Callbacks are notified with failure() for each message in the rejected batch
  3. The looper continues processing after a rejection

This ensures that messages aren't silently lost without callback notification.

Copilot uses AI. Check for mistakes.
@MichaelGHSeg MichaelGHSeg merged commit 82dfde2 into master Dec 2, 2025
10 checks passed
@MichaelGHSeg MichaelGHSeg deleted the neelkanth-kaushik/LIBRARIES-2886 branch December 2, 2025 23:59
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.

3 participants