Skip to content

TaskExecutor ScheduledExecutorService etc. cleanup#19

Merged
vorburger merged 10 commits into
enola-dev:mainfrom
vorburger:TaskExecutor-cleanupScheduler
Oct 5, 2025
Merged

TaskExecutor ScheduledExecutorService etc. cleanup#19
vorburger merged 10 commits into
enola-dev:mainfrom
vorburger:TaskExecutor-cleanupScheduler

Conversation

@vorburger
Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings October 5, 2025 00:40
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @vorburger, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the TaskExecutor by introducing a crucial time-based eviction mechanism for completed tasks, effectively addressing potential memory leaks. Concurrently, it undertakes a substantial refactoring effort to centralize and standardize the creation and management of ExecutorService and ScheduledExecutorService instances. This involves moving common utilities for virtual threads and robust uncaught exception logging into a new, dedicated dev.enola.common.concurrent package, thereby improving code maintainability, reliability, and ensuring proper resource cleanup.

Highlights

  • Task Eviction Policy: Implemented a time-based eviction policy for completed tasks within the TaskExecutor to prevent memory leaks, with a default eviction interval of one hour.
  • Executor Refactoring: Centralized and standardized the creation of ExecutorService and ScheduledExecutorService instances, moving utility methods for virtual threads and robust uncaught exception logging to a new dev.enola.common.concurrent package.
  • Improved Thread Management: Introduced named virtual threads for scheduled executors and integrated logging for uncaught exceptions in all managed executors, enhancing debuggability and reliability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

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 PR refactors the TaskExecutor and related concurrent utilities by extracting common functionality into reusable components and adding automated cleanup capabilities. The changes move away from hardcoded implementations toward a more modular and maintainable concurrent utilities framework.

  • Extracted LoggingThreadUncaughtExceptionHandler into a standalone utility class with factory method
  • Created a centralized Executors utility class for creating configured executor services with virtual threads
  • Added automated cleanup scheduling for completed tasks in TaskExecutor with configurable eviction intervals

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/dev/enola/common/concurrent/LoggingThreadUncaughtExceptionHandler.java New utility class for handling uncaught exceptions with logging support
src/dev/enola/common/concurrent/Executors.java New factory class for creating configured executor services with virtual threads
src/dev/enola/be/task/TaskExecutorServices.java Removed - functionality moved to common utilities
src/dev/enola/be/task/TaskExecutor.java Updated to use new utilities and added automated cleanup scheduling

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/dev/enola/common/concurrent/Executors.java Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a cleanup mechanism for completed tasks to prevent memory leaks and refactors executor creation into a new common utility class. The changes are a good improvement, but I've identified a critical bug in the task eviction scheduling logic that could cause incorrect behavior. I've also found some opportunities to improve maintainability by removing code duplication and an obsolete comment.

Comment thread src/dev/enola/be/task/TaskExecutor.java Outdated
Comment thread src/dev/enola/common/concurrent/Executors.java Outdated
Comment thread src/dev/enola/common/concurrent/LoggingThreadUncaughtExceptionHandler.java Outdated
@vorburger
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a good refactoring that moves executor service creation to a common utility package and adds a mechanism to clean up completed tasks, addressing a potential memory leak. The changes are generally well-structured. I have identified two high-severity issues: one in TaskExecutor related to the precision of the cleanup interval, which could prevent cleanup from running periodically for sub-second intervals, and another in the new Executors utility class concerning the use of virtual threads for a ScheduledExecutorService, which is an established anti-pattern. I've included specific code suggestions to address these points.

Comment thread src/dev/enola/be/task/TaskExecutor.java Outdated
Comment thread src/dev/enola/common/concurrent/Executors.java Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 5, 2025 00:50
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/dev/enola/be/task/TaskExecutor.java
Comment thread src/dev/enola/be/task/TaskExecutor.java
Comment thread src/dev/enola/be/task/TaskExecutor.java
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 5, 2025 00:51
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/dev/enola/be/task/TaskExecutor.java
Comment thread src/dev/enola/be/task/TaskExecutor.java
@vorburger
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the creation of ExecutorService and ScheduledExecutorService into a new common Executors utility class. It also introduces a mechanism in TaskExecutor to periodically evict completed tasks, preventing a memory leak. The changes are well-structured. My feedback focuses on improving the API of the new Executors utility for better consistency and diagnostics.

Comment thread src/dev/enola/be/task/TaskExecutor.java Outdated
Comment thread src/dev/enola/common/concurrent/Executors.java Outdated
Copilot AI review requested due to automatic review settings October 5, 2025 01:08
@vorburger
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown
Contributor

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/dev/enola/be/task/TaskExecutor.java
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 5, 2025 01:09
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a nice cleanup and improvement. It refactors the creation of ExecutorServices into a new common utility class dev.enola.common.concurrent.Executors, which improves code organization and reusability. It also introduces a periodic cleanup of completed tasks in TaskExecutor to prevent potential memory leaks, addressing a previous TODO. My review includes a critical fix for the new Executors class and a suggestion to improve the robustness of the new TaskExecutor constructor.

Comment thread src/dev/enola/common/concurrent/Executors.java
Comment thread src/dev/enola/be/task/TaskExecutor.java
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +155 to +157
cleanupScheduler.close();
timeoutScheduler.close();
executor.close();
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

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

The executor services should be shut down in reverse order of dependency to avoid potential issues. Since the executor might have tasks that could be cancelled by the timeout scheduler, consider closing the executor first, then the timeout scheduler, and finally the cleanup scheduler.

Suggested change
cleanupScheduler.close();
timeoutScheduler.close();
executor.close();
executor.close();
timeoutScheduler.close();
cleanupScheduler.close();

Copilot uses AI. Check for mistakes.
Comment on lines +61 to +63
private void evictCompletedTasks() {
tasks.values().removeIf(task -> task.status().isTerminal());
}
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

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

The evictCompletedTasks method iterates over all tasks to find completed ones. For large task maps, this could be inefficient. Consider maintaining a separate queue of completed tasks or using a time-based eviction strategy that tracks completion timestamps.

Copilot uses AI. Check for mistakes.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 5, 2025 01:11
Copy link
Copy Markdown
Contributor

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +46 to +52
public TaskExecutor(Duration completedTaskEvictionInterval) {
if (completedTaskEvictionInterval == null) {
throw new IllegalArgumentException("completedTaskEvictionInterval must not be null");
}
if (completedTaskEvictionInterval.isNegative() || completedTaskEvictionInterval.isZero()) {
throw new IllegalArgumentException("completedTaskEvictionInterval must be positive");
}
Copy link

Copilot AI Oct 5, 2025

Choose a reason for hiding this comment

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

Consider using Objects.requireNonNull() for the null check on line 47-48 for consistency with the null validation pattern used elsewhere in the codebase.

Copilot uses AI. Check for mistakes.
@vorburger vorburger added this pull request to the merge queue Oct 5, 2025
Merged via the queue into enola-dev:main with commit 618b7c7 Oct 5, 2025
1 check passed
@vorburger vorburger deleted the TaskExecutor-cleanupScheduler branch October 5, 2025 01:15
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.

2 participants