#18029: infra: fix memory leak and error handling in event listeners
stale
size: S
Cluster:
Memory Leak Fixes and Cleanup
- Add MAX_LISTENERS limit (1000) to prevent unbounded growth
- Add warning threshold at 500 listeners
- Replace silent error swallowing with error logging
- Apply fixes to agent-events, diagnostic-events, and heartbeat-events
Fixes potential memory leaks when listeners aren't properly cleaned up. Improves debuggability by logging listener errors instead of silently ignoring them.
## Summary
Describe the problem and fix in 2–5 bullets:
- Problem:
- Why it matters:
- What changed:
- What did NOT change (scope boundary):
## Change Type (select all)
- [ ] Bug fix
- [ ] Feature
- [ ] Refactor
- [ ] Docs
- [ ] Security hardening
- [ ] Chore/infra
## Scope (select all touched areas)
- [ ] Gateway / orchestration
- [ ] Skills / tool execution
- [ ] Auth / tokens
- [ ] Memory / storage
- [ ] Integrations
- [ ] API / contracts
- [ ] UI / DX
- [ ] CI/CD / infra
## Linked Issue/PR
- Closes #
- Related #
## User-visible / Behavior Changes
List user-visible changes (including defaults/config).
If none, write `None`.
## Security Impact (required)
- New permissions/capabilities? (`Yes/No`)
- Secrets/tokens handling changed? (`Yes/No`)
- New/changed network calls? (`Yes/No`)
- Command/tool execution surface changed? (`Yes/No`)
- Data access scope changed? (`Yes/No`)
- If any `Yes`, explain risk + mitigation:
## Repro + Verification
### Environment
- OS:
- Runtime/container:
- Model/provider:
- Integration/channel (if any):
- Relevant config (redacted):
### Steps
1.
2.
3.
### Expected
-
### Actual
-
## Evidence
Attach at least one:
- [ ] Failing test/log before + passing after
- [ ] Trace/log snippets
- [ ] Screenshot/recording
- [ ] Perf numbers (if relevant)
## Human Verification (required)
What you personally verified (not just CI), and how:
- Verified scenarios:
- Edge cases checked:
- What you did **not** verify:
## Compatibility / Migration
- Backward compatible? (`Yes/No`)
- Config/env changes? (`Yes/No`)
- Migration needed? (`Yes/No`)
- If yes, exact upgrade steps:
## Failure Recovery (if this breaks)
- How to disable/revert this change quickly:
- Files/config to restore:
- Known bad symptoms reviewers should watch for:
## Risks and Mitigations
List only real risks for this PR. Add/remove entries as needed. If none, write `None`.
- Risk:
- Mitigation:
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Replaced silent error swallowing with `console.error` logging in three event emitter files (`agent-events.ts`, `diagnostic-events.ts`, `heartbeat-events.ts`). Added detection for high listener counts with warnings at 500 listeners and errors at 1000 listeners, though these thresholds don't actually prevent registration.
- Improves debuggability by surfacing listener errors instead of silently ignoring them
- Adds visibility into potential memory leaks via listener count logging
- Changes are backwards compatible and don't alter event system behavior
- No test coverage for the new warning/error logging functionality
<h3>Confidence Score: 4/5</h3>
- Safe to merge with minor improvements possible
- Code changes are straightforward and improve observability without altering event system behavior. The two previously-identified issues (misleading naming and repeated warnings) are documented in existing review threads. No test coverage is the main gap, but the changes are low-risk since they only add logging.
- No files require special attention
<sub>Last reviewed commit: eaea903</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#22480: fix: memory leak, silent WS failures, and connection error handling
by Chase-Xuu · 2026-02-21
79.6%
#21463: fix(discord): prevent WebSocket death spiral + fix numeric channel ID…
by akropp · 2026-02-20
76.6%
#14719: UI: fix debug event log layout and health history toggle
by detecti1 · 2026-02-12
76.5%
#22131: fix: clear seqByRun entries in clearAgentRunContext to prevent memo...
by alanwilhelm · 2026-02-20
76.3%
#12048: fix: deduplicate config warnings to log once instead of on every re...
by mcaxtr · 2026-02-08
74.6%
#22143: Fix memory leak in WhatsApp channel reconnection loop
by lancejames221b · 2026-02-20
74.5%
#2541: fix(agents): add error handling to orphaned message cleanup
by Episkey-G · 2026-01-27
73.2%
#11250: fix: expand skills watcher ignore list and improve session repair l...
by zhangzhefang-github · 2026-02-07
73.1%
#6577: fix: add null checks for stdout/stderr when using inherit-stdio fal...
by ncmalan · 2026-02-01
72.9%
#11281: fix(logging): prevent subsystem loggers from bypassing file log lev...
by janckerchen · 2026-02-07
72.8%