Refactor ProjectViewModel resource cleanup to use addCloseable#159
Conversation
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>
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| runBlocking { | ||
| mutex.withLock { | ||
| interpreter?.close() | ||
| interpreter = null | ||
| gpuDelegate?.close() | ||
| gpuDelegate = null | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
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.