Skip to content

Memory leak in MessagingService._delete() method #9062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shehryar-turing opened this issue May 27, 2025 · 2 comments
Open

Memory leak in MessagingService._delete() method #9062

shehryar-turing opened this issue May 27, 2025 · 2 comments

Comments

@shehryar-turing
Copy link

Bug Description

The MessagingService._delete() method in the Firebase Messaging SDK has a memory leak issue where resources are not properly cleaned up when the service is deleted.

Root Cause

The current implementation of MessagingService._delete() only returns Promise.resolve() without cleaning up:

  1. Timer leak: The logging service starts a recursive setTimeout loop via _processQueue() that continues indefinitely
  2. Event listener leak: Event listeners added in factory functions are never removed
  3. Missing cleanup: No cleanup of message handlers, log events, or service worker references

Impact

  • Memory leaks when Firebase messaging service is deleted
  • Timers continue running in the background
  • Event listeners remain attached
  • Potential performance degradation over time

Affected Code

  • packages/messaging/src/messaging-service.ts - _delete() method
  • packages/messaging/src/helpers/logToFirelog.ts - _processQueue() timer management
  • packages/messaging/src/helpers/register.ts - Event listener registration

Comparison with Other Services

Other Firebase services like AppCheckService properly clean up their resources in _delete() methods by removing listeners and clearing state.

Proposed Solution

Implement proper resource cleanup in MessagingService._delete() including:

  • Clear logging timers
  • Remove event listeners
  • Reset all properties to initial state
  • Clear message handlers and log events
  • Provide cleanup mechanism for registered cleanup functions

Environment

  • Firebase JS SDK version: 11.8.1
  • Affects both browser and service worker environments
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@looptheloop88
Copy link

Hi @shehryar-turing, thanks for the report. Let me bring this matter to the attention of our engineers.

The MessagingService._delete() method only returns promise

_delete(): Promise<void> {
return Promise.resolve();
}

While the AppCheckService._delete() method, for comparison, has implementation to remove listeners and clear states

_delete(): Promise<void> {
const { tokenObservers } = getStateReference(this.app);
for (const tokenObserver of tokenObservers) {
removeTokenListener(this.app, tokenObserver.next);
}
return Promise.resolve();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants