-
Notifications
You must be signed in to change notification settings - Fork 40
Improve ExecutorService lifecycle handling by moving shutdown to finally block #3166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,20 +105,21 @@ public ExportReport startExport( | |
| isFirstBatch, | ||
| exportReport)); | ||
| } | ||
| executorService.shutdown(); | ||
| if (executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) { | ||
| logger.info("All tasks completed"); | ||
| } else { | ||
| logger.error("Timeout occurred while waiting for tasks to complete"); | ||
| // TODO: handle this | ||
| } | ||
| processFooter(exportOptions, tableMetadata, bufferedWriter); | ||
| } catch (InterruptedException | ||
| | IOException | ||
| | UnknownTransactionStatusException | ||
| | CrudException e) { | ||
| } catch (IOException | UnknownTransactionStatusException | CrudException e) { | ||
| logger.error("Error during export: ", e); | ||
| } finally { | ||
| executorService.shutdown(); | ||
| try { | ||
| if (executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) { | ||
| logger.info("All tasks completed"); | ||
| } else { | ||
| logger.error("Timeout occurred while waiting for tasks to complete"); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| logger.error("Interrupted while waiting for executor termination", e); | ||
| } | ||
|
Comment on lines
+112
to
+121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in my other comment, try {
if (executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
logger.info("All tasks completed");
processFooter(exportOptions, tableMetadata, bufferedWriter);
} else {
logger.error("Timeout occurred while waiting for tasks to complete");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("Interrupted while waiting for executor termination", e);
} catch (IOException e) {
logger.error("Error writing footer: ", e);
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made changes based on feedback in e84bc49. |
||
| bufferedWriter.flush(); | ||
| } | ||
| } catch (ExportOptionsValidationException | IOException | ScalarDbDaoException e) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call to
processFooterintroduces a race condition. It's now executed before the tasks in theExecutorServiceare guaranteed to be complete. For formats like JSON, this can lead to a corrupted output file (e.g., a closing]written before all objects). This line should be removed. The call should be moved into thefinallyblock afterawaitTerminationsucceeds, as I've suggested in another comment.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made changes based on feedback in e84bc49.