Repository navigation
Fixes memory leak in ScreenInstrumentation - #16906
Conversation
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 the 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 counterproductive. 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. |
|
/gemini review |
Generated by 🚫 Danger |
There was a problem hiding this comment.
Code Review
This pull request refactors ScreenInstrumentation to manage its asynchronous tasks, introducing a listeningTask for notification tracking, handling deinitialization to cancel active tasks, and adding a unit test to verify clean deallocation. The review feedback suggests simplifying the initialization of ScreenInstrumentation by spawning the listeningTask synchronously inside init rather than wrapping it in an outer Task that calls configure(with:). This change would eliminate unnecessary actor hops and allow for the removal of a sleep buffer in the deallocation unit test.
ncooke3
left a comment
There was a problem hiding this comment.
Consideration: evaluate if configure's implementation needs to handle multiple callers, to prevent tasks getting overwritten
Summary
Fixes a memory leak and notification subscription race in ScreenInstrumentation.
Problem
ScreenInstrumentation.configure()strongly captured self inside an infinite for await loop and was never stored or cancelled.reportingTaskalso strongly captured self across its 50ms debounce sleep. As a result,ScreenInstrumentationinstances never deallocated and continued processing notifications indefinitely.NotificationCenter.default.notifications(named: .screenTrackingEvent)was created asynchronously insideTask(priority: .utility), causing notifications posted immediately afterScreenInstrumentation.initto be missed before the task started.Fix
NotificationCenter.Notificationsstream synchronously in init so observer registration happens immediately, eliminating theTask.sleepworkarounds in tests.listeningTaskproperty, capture[weak self]in bothlisteningTaskandreportingTask, and cancel both tasks in deinit.ScreenInstrumentationdeallocates and stops processing notifications when released.