Skip to content

fix: clear timeout after promise rejection#24864

Merged
Hona merged 1 commit intoanomalyco:devfrom
Hona:fix/timeout-clear-on-reject
Apr 28, 2026
Merged

fix: clear timeout after promise rejection#24864
Hona merged 1 commit intoanomalyco:devfrom
Hona:fix/timeout-clear-on-reject

Conversation

@Hona
Copy link
Copy Markdown
Member

@Hona Hona commented Apr 28, 2026

Summary

  • Clear withTimeout timers when the wrapped promise rejects, not only when it fulfills.
  • Keeps timeout behavior unchanged while avoiding bounded leftover timers on fast failure paths.

Repro

I reproduced this with 10,000 rejected promises wrapped in withTimeout(..., 60_000).

Before this change, all promises settled quickly, but the process stayed alive because the 60s timers were still pending.

After this change, the same repro exits immediately after the rejected promises settle.

User-Facing Impact

This does not make successful actions faster. It only matters when a wrapped operation rejects quickly before its timeout.

Action If this fails fast Possible symptom before this fix
MCP connect/list tools bad MCP server/config rejects immediately process may linger up to 30s after it otherwise finished
LSP initialize language server rejects/crashes during initialize process may linger up to 45s if you exit right after
LSP diagnostics diagnostic request rejects process may linger up to 3s
TUI shutdown worker RPC worker shutdown RPC rejects immediately opencode exit may hang up to 5s

In normal long-running TUI/server usage, users probably will not notice. This is mostly an opencode does not exit promptly after a failure cleanup bug, not action latency.

Scope

This is a bounded timer/process-exit leak, not an unbounded native memory leak. The affected callsites are MCP connect/list-tools, LSP initialize/diagnostics, and TUI worker shutdown.

Verification

  • Focused Bun repro: 10,000 rejected promises wrapped in withTimeout(..., 60_000) exits in under 1s
  • bun typecheck from packages/opencode
  • pre-push hook: bun turbo typecheck

Copilot AI review requested due to automatic review settings April 28, 2026 23:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a process-liveness leak in withTimeout by ensuring the timeout handle is cleared when the wrapped promise rejects, not just when it fulfills. This prevents large batches of fast-failing operations from leaving long-lived timers pending and keeping the process alive.

Changes:

  • Replace promise.then(...) with promise.finally(...) so clearTimeout(timeout) runs on both fulfill and reject paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Hona Hona enabled auto-merge (squash) April 28, 2026 23:33
@Hona Hona merged commit 9fbeafb into anomalyco:dev Apr 28, 2026
17 checks passed
afanty2021 added a commit to afanty2021/opencode that referenced this pull request Apr 29, 2026
Upstream commits:
- fix: clear timeout after promise rejection (anomalyco#24864)
- chore: generate
- fix(bash): memory leak - release parsed syntax trees (anomalyco#24861)
- feat(core): filter sessions by path and add setting to disable (anomalyco#24849)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

# Conflicts:
#	packages/core/CLAUDE.md
#	packages/core/src/util/CLAUDE.md
oleksii-honchar pushed a commit to oleksii-honchar/better-opencode that referenced this pull request Apr 29, 2026
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.

2 participants