Skip to content

Conversation

@paul-kraftlauget
Copy link

This PR

  • fixes a memory leak in InProcessResolver

Related Issues

Fixes #1677

How to test

Please see the unit test in this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @paul-kraftlauget, 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 addresses a critical memory leak in the InProcessResolver by implementing a robust thread shutdown mechanism. The changes ensure that a background thread responsible for watching state changes is properly terminated when the resolver is no longer needed, preventing it from consuming resources indefinitely. This improves the overall stability and resource management of the application.

Highlights

  • Thread Shutdown Mechanism: Introduced an AtomicBoolean named shutdown in InProcessResolver to manage the termination state of the stateWatcher thread.
  • Memory Leak Fix: Modified the stateWatcher thread's main loop condition from while (true) to while (!shutdown.get()), ensuring the thread gracefully exits when the resolver is shut down.
  • Shutdown Method Enhancement: Implemented shutdown.set(true) within the shutdown() method of InProcessResolver to signal the stateWatcher thread to terminate, preventing resource leaks.
  • Unit Test for Thread Cleanup: Added a new unit test, testStateWatcherThreadIsCleanedUpDuringShutdown, to InProcessResolverTest to verify that the stateWatcher thread is properly terminated and cleaned up upon calling shutdown().
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
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 fixes a thread leak in InProcessResolver by introducing a shutdown flag to allow the stateWatcher thread to terminate gracefully. The change is correct and addresses the issue.

My review includes two main points for improvement:

  1. In InProcessResolver.java, I've suggested a more robust shutdown pattern using thread interruption. This would make the component's shutdown logic more self-contained and less reliant on the behavior of downstream components.
  2. In InProcessResolverTest.java, I've pointed out that the new test for the shutdown logic isn't fully representative of the production scenario, as it manually unblocks the thread instead of verifying that the shutdown process does so.

These suggestions aim to improve the robustness and test quality of the solution.

final Thread stateWatcher = new Thread(() -> {
try {
while (true) {
while (!shutdown.get()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using a shutdown flag is a good step towards graceful shutdown. However, the thread will remain blocked on flagStore.getStateQueue().take() until an item is put on the queue. The current design relies on flagStore.shutdown() to eventually cause this.

A more robust and direct approach for shutting down a worker thread that blocks on a queue is to use thread interruption. This avoids relying on the indirect behavior of downstream components.

Consider this pattern:

  1. Store the stateWatcher thread in an instance field.
  2. In the shutdown() method, call interrupt() on the thread instance.
  3. The take() method will throw InterruptedException, which is already caught, and the thread will terminate.

This makes the shutdown logic of InProcessResolver self-contained and not dependent on the implementation details of FlagStore. It also handles cases where flagStore.shutdown() might fail or not behave as expected.

Copy link
Member

Choose a reason for hiding this comment

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

While this is true, what's been added here is an objective improvement.

@paul-kraftlauget ... up to you if you'd like to go further. I do think this comment is correct

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it was correct. And looking at the file sync implementation, there wouldn't be another event sent when shutdown, so the leak still would occur. I have pushed an update now. I hope it is ok.

@paul-kraftlauget paul-kraftlauget changed the title Fixed thread memory leak in InProcessResolver fix: Fixed thread memory leak in InProcessResolver Jan 9, 2026
@toddbaert toddbaert changed the title fix: Fixed thread memory leak in InProcessResolver fix: thread memory leak in InProcessResolver Jan 9, 2026
@toddbaert
Copy link
Member

@paul-kraftlauget Thanks a lot! can you fix the DCO failure? You can do a git commit --amend --signoff which just gives a legal signoff to the commit.

@paul-kraftlauget paul-kraftlauget force-pushed the bugfix/memory-leak-in-InProcessResolver branch from fdb1905 to 7142c1d Compare January 9, 2026 21:43
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.

Thread memory leak in flagd InProcessResolver

6 participants