Skip to content

Refactor ProjectViewModel resource cleanup to use addCloseable#159

Merged
harrydbarnes merged 1 commit into
mainfrom
refactor-projectviewmodel-cleanup-12746094443311489517
Jan 28, 2026
Merged

Refactor ProjectViewModel resource cleanup to use addCloseable#159
harrydbarnes merged 1 commit into
mainfrom
refactor-projectviewmodel-cleanup-12746094443311489517

Conversation

@harrydbarnes

Copy link
Copy Markdown
Owner

Replaces the GlobalScope.launch based cleanup in ProjectViewModel.onCleared with the idiomatic addCloseable mechanism. Modified FaceRecognitionHelper to implement Closeable and updated its close method to be non-suspending using runBlocking, ensuring safe resource release.

Replaces the GlobalScope.launch based cleanup in ProjectViewModel.onCleared with the idiomatic addCloseable mechanism.
Modified FaceRecognitionHelper to implement Closeable and updated its close method to be non-suspending using runBlocking, ensuring safe resource release.

Co-authored-by: harrydbarnes <145344818+harrydbarnes@users.noreply.github.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @harrydbarnes, 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 improves resource management within the application by transitioning the ProjectViewModel's cleanup strategy from a manual, potentially error-prone GlobalScope.launch implementation to the more robust and integrated addCloseable mechanism. This change ensures that the FaceRecognitionHelper's resources are reliably released when the ViewModel is no longer needed, enhancing application stability and preventing resource leaks.

Highlights

  • Resource Cleanup Refactor: The pull request refactors the resource cleanup mechanism in ProjectViewModel to utilize the idiomatic addCloseable method, replacing the previous GlobalScope.launch based approach.
  • FaceRecognitionHelper implements Closeable: The FaceRecognitionHelper class now implements the java.io.Closeable interface, making it compatible with the addCloseable mechanism.
  • Non-suspending Close Method: The close() method within FaceRecognitionHelper has been modified to be non-suspending by wrapping its coroutine logic in runBlocking, ensuring it adheres to the Closeable interface contract for synchronous resource release.
  • Simplified ViewModel Lifecycle: The manual cleanup logic in ProjectViewModel.onCleared() has been removed, as addCloseable now automatically handles the faceRecognitionHelper's lifecycle, leading to cleaner and more robust code.
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.

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

Copy link
Copy Markdown
Contributor

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 resource cleanup for ProjectViewModel to use the idiomatic addCloseable mechanism, which is a cleaner approach for ViewModel lifecycle management and removes GlobalScope.launch for cleanup. However, a critical threading issue and potential Denial of Service (DoS) vulnerability have been identified in FaceRecognitionHelper.close(). The use of runBlocking on the main thread to acquire a mutex, especially when background tasks might be holding it, risks triggering ANR (Application Not Responding) errors and creating a performance bottleneck. It is recommended to perform the cleanup asynchronously or ensure the close() operation does not block the UI thread.

Comment on lines +116 to 123
runBlocking {
mutex.withLock {
interpreter?.close()
interpreter = null
gpuDelegate?.close()
gpuDelegate = null
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-medium medium

The use of runBlocking within the close() method, which is called on the main thread via addCloseable in ProjectViewModel, introduces a potential Denial of Service (DoS) vulnerability. If a background task is performing face recognition and holding the mutex, the main thread will be blocked until the computationally intensive TFLite inference completes, leading to Application Not Responding (ANR) errors. This also presents a significant performance bottleneck, as runBlocking on the main thread should generally be avoided for potentially blocking or intensive operations. Consider if interpreter?.close() or gpuDelegate?.close() are truly fast and non-blocking; if not, a different resource management strategy or offloading these operations to a background thread within close() might be more appropriate.

@harrydbarnes harrydbarnes merged commit a6198e9 into main Jan 28, 2026
1 check passed
@harrydbarnes harrydbarnes deleted the refactor-projectviewmodel-cleanup-12746094443311489517 branch January 28, 2026 12:54
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.

1 participant