Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This call to processFooter introduces a race condition. It's now executed before the tasks in the ExecutorService are 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 the finally block after awaitTermination succeeds, as I've suggested in another comment.

Copy link
Contributor Author

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.

} 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
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

As mentioned in my other comment, processFooter should be called here, after awaitTermination has successfully completed. This ensures all data has been written before the footer is added. The suggestion below moves the call here and also adds the necessary handling for IOException, which processFooter can throw.

        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);
        }

Copy link
Contributor Author

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.

bufferedWriter.flush();
}
} catch (ExportOptionsValidationException | IOException | ScalarDbDaoException e) {
Expand Down
Loading