Skip to content

Make global uncaughtException/unhandledRejection handlers opt-in to background-agent feature #3856

Closed

Description

@dhirajbhasme228

Summary

The process-cleanup module unconditionally registers uncaughtException and unhandledRejection handlers on the host Node process whenever any CleanupManager is created (src/features/background-agent/process-cleanup.ts, function registerManagerForCleanup). On any unhandled error/rejection these handlers call scheduleForcedExit(handler(error), 1, true) → process.exit(1), terminating the entire opencode process.

After PR #3639 (which I appreciate is intentional for users actively running background agents), this kills opencode for users who load the plugin but don't use background agents. Transient streaming errors from upstream LLM endpoints — e.g. SocketError { code: 'UND_ERR_SOCKET' } from undici when api.githubcopilot.com resets a long stream — surface as unhandled rejections in opencode's session.processor Effect runtime and trip these global handlers, exiting the app mid-response. This happens repeatedly on flaky networks/proxies and gives users a "opencode just dies after every response" experience.

This is distinct from #3731 (re-entrant log loop) — that fix prevents log spam during cleanup, but the forced exit on every transient unhandled rejection remains.

Reproduction

  1. Install oh-my-openagent (current ^4.0.0) into an opencode config dir, list it under plugin in opencode.json.
  2. Configure a github-copilot/* model (or any provider with occasional transient socket resets).
  3. Send a long streaming prompt over a flaky connection (corporate proxy, hotspot, etc.) so undici sees a mid-stream socket reset.
  4. opencode receives an AI_APICallError { cause: SocketError UND_ERR_SOCKET }. The error propagates as an unhandled rejection. The plugin's listener fires and calls process.exit(1). The entire opencode process exits — the desktop window closes, the CLI returns to the shell prompt — even when the user never invoked any background-agent functionality.

The same applies to any unhandled rejection in opencode itself or any other plugin: the moment one bubbles up uncaught, the host dies.

Why I think this should be opt-in

The process-cleanup machinery is conceptually tied to the background-agent feature (src/features/background-agent/process-cleanup.ts), but registerManagerForCleanup is called unconditionally whenever a CleanupManager is constructed. A user who installs the plugin only for the multi-agent presets / model fallback / LSP tools effectively has no opt-out from the global handlers, and gets none of the benefit of background-agent forced cleanup either.

Suggested fix

One of:

  1. Feature-gate the global handler registration behind whatever flag enables background agents (config option, command-line flag, or only register when the first background-agent task is actually started rather than at plugin load).
  2. Provide an explicit opt-out env var like OMO_DISABLE_PROCESS_CLEANUP=1 that short-circuits registerManagerForCleanup (and document it in the troubleshooting README).
  3. Default to process.exitCode = 1 (no process.exit) so the process exits naturally only when the event loop drains, and let opencode's own error handling decide whether to recover, log, and continue. The forced exit can still be opt-in for the bg-agent users who depend on it.

(1) seems cleanest. (2) is the lowest-risk near-term mitigation for impacted users.

Environment

  • oh-my-openagent 4.0.0 (also reproduced on 3.17.14)
  • opencode 1.14.41 (Electron desktop) and 1.14.39 (npm CLI)
  • macOS 15.x, Node 24.x
  • Provider: github-copilot

Code references

  • Listener registration: src/features/background-agent/process-cleanup.ts — registerManagerForCleanup → cleanupErrorHandlers.set("uncaughtException", registerErrorEvent("uncaughtException", cleanupAll)) and the equivalent unhandledRejection line
  • Forced exit: same file, scheduleForcedExit calling process.exit(exitCode) after the cleanup promise resolves

Thanks for the active maintenance here — the recent work on #3639 / #3731 / #3793 shows you're already thinking carefully about this surface, and I'm hopeful a feature gate is a small ergonomic win for users in the unfortunate "loaded but innocent" position.

Activity

  1. ottosulin commented on May 14, 2026

    @ottosulin

    +1, hit this on opencode Desktop v1.14.50 with [email protected] and @4.1.2. Sidecar exits silently with code: 1 between 30 sec and ~10 min after server-ready, no stderr in opencode's main.log. The actual error (in my case a DrizzleError: file is not a database from opencode's own session processor, unrelated to the plugin) is logged only to $TMPDIR/oh-my-opencode.log, then scheduleForcedExit(…, 1, true) → process.exit(1). Removing the plugin from opencode.json makes the sidecar stay alive indefinitely. Cross-referencing my opencode-side report: anomalyco/opencode#27557.

    Strongly in favor of option (1) — feature-gate the global handler registration so users who only use the multi-agent presets don't pay for behavior they don't need.

  2. added 2 commits that reference this issue on May 15, 2026
    dd68f32
    f00a693
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions