opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Memory leak: SDK event listeners never unsubscribed in App component

Open sauerdaniel opened this issue 4 days ago • 1 comments

Problem

The TUI App component subscribes to 6 SDK events but never unsubscribes from them:

  • TuiEvent.CommandExecute
  • TuiEvent.ToastShow
  • TuiEvent.SessionSelect
  • SessionApi.Event.Deleted
  • SessionApi.Event.Error
  • Installation.Event.UpdateAvailable

The sdk.event.on() method returns an unsubscribe function that is being ignored.

Code Location

packages/opencode/src/cli/cmd/tui/app.tsx - lines 557-616

Impact

  • Event listeners accumulate over component lifecycle
  • Closures retain references preventing garbage collection
  • Memory growth in long-running sessions
  • Potential duplicate event handling

Relates to #5363

Expected Behavior

Event listeners should be stored and unsubscribed in onCleanup:

const unsubs: (() => void)[] = []

unsubs.push(sdk.event.on(TuiEvent.CommandExecute.type, (evt) => {
  command.trigger(evt.properties.command)
}))

// ... other listeners

onCleanup(() => {
  unsubs.forEach((unsub) => unsub())
})

sauerdaniel avatar Jan 13 '26 17:01 sauerdaniel