firebase / firebase/firebase-tools

PubSub Emulator Memory Leak: Node Processes Persist After Function Execution

Open
#8,113 5 comments 2 reactions 0 assignees View on GitHub
emulator-suite emulators: pubsub type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info

**firebase-tools:** 13.29.1
**Platform:** macOS
**Node.js Version:** v20.18.1

### [REQUIRED] Description

The Firebase PubSub emulator creates a new Node process for each PubSub event, but these processes are not being terminated after function execution completes. This leads to memory exhaustion as more events are processed, making local development difficult and potentially causing system stability issues.

### [REQUIRED] Test case

Reproduction repository: https://github.com/kdawgwilk/pubsub-emulator-bug-repro

### [REQUIRED] Steps to reproduce

1. Clone the reproduction repository
2. Install dependencies:
```bash
cd functions
npm install
```
3. Start the Firebase emulators:
```bash
npm run serve -- --project
```
4. In a separate terminal, run the test script that publishes 100 messages:
```bash
GCLOUD_PROJECT= \
FIREBASE_AUTH_EMULATOR_HOST=localhost:9099 \
FIRESTORE_EMULATOR_HOST=localhost:8080 \
PUBSUB_EMULATOR_HOST=localhost:8085 \
npx tsx ./functions/scripts/test_pubsub.ts
```
5. Open Activity Monitor (macOS) and observe:
- Multiple Node processes being created
- Processes remain active even after function execution
- Memory usage increases with each new message
- Processes do not terminate automatically

### [REQUIRED] Expected behavior

- Node processes should be created when handling PubSub events
- Processes should terminate automatically after function execution completes
- System resources should be properly cleaned up
- Memory usage should remain stable across multiple events

### [REQUIRED] Actual behavior

- New Node processes are spawned for each PubSub event
- Processes persist indefinitely after function completion
- Memory usage grows continuously as new messages are processed
- No automatic cleanup of completed function processes
- System resources are exhausted if running for extended periods

Image

### Impact

This issue significantly impacts local development by:
- Consuming system resources unnecessarily
- Requiring manual intervention to clean up processes
- Potentially causing system slowdown or crashes during extended development sessions
- Making it difficult to run long-term tests or process multiple messages

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.