firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Memory leak in MessagingService._delete() method

Open shehryar-turing opened this issue 6 months ago • 2 comments

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

shehryar-turing avatar May 27 '25 07:05 shehryar-turing

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

google-oss-bot avatar May 27 '25 07:05 google-oss-bot

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

https://github.com/firebase/firebase-js-sdk/blob/d5e5795c944930eb8df424c3e9bc33e17ab00b84/packages/messaging/src/messaging-service.ts#L63-L65

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

https://github.com/firebase/firebase-js-sdk/blob/d5e5795c944930eb8df424c3e9bc33e17ab00b84/packages/app-check/src/factory.ts#L38-L45

looptheloop88 avatar May 27 '25 12:05 looptheloop88