Skip to content

Conversation

@sauerdaniel
Copy link
Contributor

@sauerdaniel sauerdaniel commented Jan 17, 2026

Summary

Fix memory leaks from event listeners not being properly cleaned up in TUI components and the Slack bot.

Fixes #9155

Problems

1. TUI Sync Context (sync.tsx)

The sdk.event.listen() call sets up event listeners but the returned unsubscribe function is never called.

2. Tooltip Component (tooltip.tsx)

Mouse event listeners are added to elements but never removed on component unmount.

3. Slack Bot (slack/index.ts)

  • Sessions Map grows without bounds
  • No cleanup of old/stale sessions
  • No graceful shutdown handling

Solution

TUI Sync Context

Store and call unsubscribe function in onCleanup:

const unsubscribe = sdk.event.listen((e) => { ... })
onCleanup(unsubscribe)

Tooltip Component

Add proper cleanup in onCleanup handler.

Slack Bot

  • Add lastUsed timestamp to sessions
  • Add periodic cleanup of sessions older than 1 hour
  • Add MAX_SESSIONS limit (100)
  • Add graceful shutdown handlers for SIGINT/SIGTERM

Changes

  • packages/opencode/src/cli/cmd/tui/context/sync.tsx - Store and call unsubscribe
  • packages/ui/src/components/tooltip.tsx - Add event listener cleanup
  • packages/slack/src/index.ts - Add session management and shutdown handlers

Testing

  • TypeScript compilation passes (bun turbo typecheck)
  • Unit tests pass (725 tests, 0 failures)

Note: Manual TUI and Slack bot testing was not performed. Memory leak verification requires runtime memory profiling.

Clean up SDK event listeners on unmount in TUI sync context and
improve Slack bot session management to prevent leaks.

TUI changes (sync.tsx):
- Store unsubscribe function from sdk.event.listen()
- Call unsubscribe in onCleanup handler

Slack changes:
- Add lastUsed timestamp to sessions
- Add periodic cleanup of old sessions (1 hour timeout)
- Add graceful shutdown handlers for SIGINT/SIGTERM
- Limit max sessions to prevent unbounded growth
@github-actions
Copy link
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
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate PRs Found

  1. fix(tui): clean up SDK event listeners on unmount #8254 - fix(tui): clean up SDK event listeners on unmount

  2. fix(core): add dispose functions to prevent subscription memory leaks #7032 - fix(core): add dispose functions to prevent subscription memory leaks

These PRs appear to target the same memory leak issues, particularly around TUI event listener cleanup. Review their implementation status to ensure no duplicated work.

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.

Memory leak: TUI and Slack bot event listeners not cleaned up

1 participant