Fix potential ANR by making FaceRecognitionHelper.close non-blocking#160
Fix potential ANR by making FaceRecognitionHelper.close non-blocking#160harrydbarnes wants to merge 1 commit into
Conversation
Refactored `FaceRecognitionHelper.close()` to execute cleanup operations (TFLite interpreter and GPU delegate closure) asynchronously on `Dispatchers.IO` instead of using `runBlocking` on the calling thread. This prevents main-thread blocking and potential ANRs when `ProjectViewModel` cleans up resources while a long-running inference might be holding the mutex. Changes: - Removed `runBlocking` in `close()`. - Launched a coroutine on `Dispatchers.IO` to acquire the lock and close resources. - Added necessary imports (`CoroutineScope`, `Dispatchers`, `launch`). 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 addresses a potential ANR issue by modifying 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 effectively resolves a potential Application Not Responding (ANR) issue by refactoring the FaceRecognitionHelper.close() method to be non-blocking. The replacement of runBlocking with an asynchronous coroutine launch is a solid improvement. I have one suggestion to further enhance the implementation by aligning it with structured concurrency best practices for managing coroutine scopes.
|
|
||
| override fun close() { | ||
| runBlocking { | ||
| CoroutineScope(Dispatchers.IO).launch { |
There was a problem hiding this comment.
While this change correctly makes the close operation non-blocking, creating a new CoroutineScope on each call is not ideal. It's a better practice for a class that launches coroutines to own and manage a single CoroutineScope throughout its lifecycle. This avoids the overhead of creating new scopes and aligns with structured concurrency principles.
A more robust approach would be to define a scope as a property of the FaceRecognitionHelper class.
Example:
// At the class level, with other properties
private val helperScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
// Then, in your close() method
helperScope.launch { ... }This would centralize coroutine management and make the helper's asynchronous operations more manageable and efficient.
Refactored
FaceRecognitionHelper.close()to execute cleanup operations (TFLite interpreter and GPU delegate closure) asynchronously onDispatchers.IOinstead of usingrunBlockingon the calling thread. This prevents main-thread blocking and potential ANRs whenProjectViewModelcleans up resources while a long-running inference might be holding the mutex.Changes:
runBlockinginclose().Dispatchers.IOto acquire the lock and close resources.CoroutineScope,Dispatchers,launch).