Skip to content

fix(tui): cleanup event listeners on component unmount to prevent MaxListenersExceededWarning#34616

Open
RuszLi wants to merge 2 commits into
anomalyco:devfrom
RuszLi:fix/maxlisteners-leak
Open

fix(tui): cleanup event listeners on component unmount to prevent MaxListenersExceededWarning#34616
RuszLi wants to merge 2 commits into
anomalyco:devfrom
RuszLi:fix/maxlisteners-leak

Conversation

@RuszLi

@RuszLi RuszLi commented Jun 30, 2026

Copy link
Copy Markdown

Issue for this PR

Fixes #34617

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

MaxListenersExceededWarning fires when 11+ event listeners accumulate on a single EventEmitter in the TUI. The stack trace shows it happens inside SolidJS reactive components during effect evaluation.

event.on() handlers in Session, App, and Prompt components were not cleaned up via onCleanup(). Each time a component remounts (e.g., switching sessions), a new listener is added without removing the old one. After ~5 session switches the default Node.js maxListeners: 10 limit is exceeded.

Fix: capture the unsubscribe function returned by event.on() and pass it to onCleanup().

How did you verify your code works?

  • event.on() returns an unsubscribe function (see packages/tui/src/context/event.ts:26)
  • Other components in the same codebase already follow this pattern correctly (e.g., theme.tsx:226 uses onCleanup to remove renderer event listeners)
  • The fix is a mechanical refactor — no logic change

Screenshots / recordings

N/A — internal code change, no UI impact.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…ListenersExceededWarning

event.on() handlers in Session, App, and Prompt components were not
being cleaned up via onCleanup(), causing listeners to accumulate each
time the component remounted (e.g. switching sessions). After ~5 session
switches the default Node.js maxListeners limit of 10 was exceeded,
triggering MaxListenersExceededWarning.

Fix: capture the unsubscribe function returned by event.on() and pass
it to onCleanup() so listeners are removed when the component unmounts.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jun 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(tui): event listeners accumulate on component remount causing MaxListenersExceededWarning

1 participant