Memory leak in MessagingService._delete() method
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:
- Timer leak: The logging service starts a recursive
setTimeoutloop via_processQueue()that continues indefinitely - Event listener leak: Event listeners added in factory functions are never removed
- 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()methodpackages/messaging/src/helpers/logToFirelog.ts-_processQueue()timer managementpackages/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
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
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