Skip to content

Fixes memory leak in ScreenInstrumentation - #16906

Merged
mghaznav merged 2 commits into
firebase-crashlytics-telemetryfrom
ali/fix-screen-inst
Oct 15, 2026
Merged

mghaznav merged 2 commits into
firebase-crashlytics-telemetryfrom
ali/fix-screen-inst

Conversation

@mghaznav

Copy link
Copy Markdown
Contributor

Summary

Fixes a memory leak and notification subscription race in ScreenInstrumentation.

Problem

  1. Memory Leak: The background notification-listening Task in ScreenInstrumentation.configure() strongly captured self inside an infinite for await loop and was never stored or cancelled. reportingTask also strongly captured self across its 50ms debounce sleep. As a result, ScreenInstrumentation instances never deallocated and continued processing notifications indefinitely.
  2. Missed Early Notifications: NotificationCenter.default.notifications(named: .screenTrackingEvent) was created asynchronously inside Task(priority: .utility), causing notifications posted immediately after ScreenInstrumentation.init to be missed before the task started.

Fix

  1. Create the NotificationCenter.Notifications stream synchronously in init so observer registration happens immediately, eliminating the Task.sleep workarounds in tests.
  2. Store the notification-listening task in an actor-isolated listeningTask property, capture [weak self] in both listeningTask and reportingTask, and cancel both tasks in deinit.
  3. Add a unit test verifying that ScreenInstrumentation deallocates and stops processing notifications when released.

@mghaznav mghaznav self-assigned this Oct 14, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor
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 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 .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 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.

@mghaznav

Copy link
Copy Markdown
Contributor Author

/gemini review

@danger-firebase-ios

Copy link
Copy Markdown
1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

@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 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 ncooke3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consideration: evaluate if configure's implementation needs to handle multiple callers, to prevent tasks getting overwritten

@mghaznav
mghaznav merged commit 39ed552 into firebase-crashlytics-telemetry Oct 15, 2026
12 checks passed
@mghaznav
mghaznav deleted the ali/fix-screen-inst branch October 15, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants