[PR #10846] fix: memory leaks and hanging issues #13580

Closed
opened 2026-02-16 18:18:25 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/10846

State: closed
Merged: No


Summary

This PR fixes several memory leaks and hanging issues in OpenCode:

RPC Error Propagation (Hang Prevention)

  • rpc.ts: Server-side errors now properly propagate back to client via rpc.error messages
  • rpc.ts: Added rejectAll() method to reject all pending requests on worker crash
  • rpc.ts: Added 60s timeout for RPC requests to prevent infinite hangs
  • thread.ts: Worker crash now calls rejectAll() to fail all pending RPCs

Memory Leak Fixes

  • acp.ts: Consolidated stdin listeners (was creating new listener per message)
  • mcp/index.ts: Cleanup stdout/stderr listeners when subprocess closes
  • mdns.ts: Track and cleanup service listeners on stop
  • worker.ts: Cleanup GlobalBus listener on worker shutdown
  • thread.ts: Cleanup terminal on worker crash, add SIGINT/SIGTERM handlers

Event Stream Resilience

  • worker.ts: Added error logging, consecutive error tracking, and exponential backoff

SDK Timeout Protection

  • agent.ts, session.ts, run.ts: Wrap SDK calls with 60s timeout to prevent indefinite hangs

Root Cause

The main hanging issue was caused by RPC having NO error propagation - when worker-side code threw an exception, the error was never sent back to the client, causing await calls to hang indefinitely.

Testing

  • TypeScript compilation passes
  • Manual testing of TUI startup/shutdown cycles
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/10846 **State:** closed **Merged:** No --- ## Summary This PR fixes several memory leaks and hanging issues in OpenCode: ### RPC Error Propagation (Hang Prevention) - **rpc.ts**: Server-side errors now properly propagate back to client via `rpc.error` messages - **rpc.ts**: Added `rejectAll()` method to reject all pending requests on worker crash - **rpc.ts**: Added 60s timeout for RPC requests to prevent infinite hangs - **thread.ts**: Worker crash now calls `rejectAll()` to fail all pending RPCs ### Memory Leak Fixes - **acp.ts**: Consolidated stdin listeners (was creating new listener per message) - **mcp/index.ts**: Cleanup stdout/stderr listeners when subprocess closes - **mdns.ts**: Track and cleanup service listeners on stop - **worker.ts**: Cleanup GlobalBus listener on worker shutdown - **thread.ts**: Cleanup terminal on worker crash, add SIGINT/SIGTERM handlers ### Event Stream Resilience - **worker.ts**: Added error logging, consecutive error tracking, and exponential backoff ### SDK Timeout Protection - **agent.ts, session.ts, run.ts**: Wrap SDK calls with 60s timeout to prevent indefinite hangs ## Root Cause The main hanging issue was caused by RPC having NO error propagation - when worker-side code threw an exception, the error was never sent back to the client, causing `await` calls to hang indefinitely. ## Testing - TypeScript compilation passes - Manual testing of TUI startup/shutdown cycles
yindo added the pull-request label 2026-02-16 18:18:25 -05:00
yindo closed this issue 2026-02-16 18:18:25 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13580