GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(opencode): prevent resource leaks in serve/web shutdown and SSE stream cleanup by s2bomb · Pull Request #14092 · anomalyco/opencode

Conversation

@s2bomb

@s2bomb s2bomb commented Feb 18, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #14091

Two resource cleanup bugs in the serve/web commands and the SSE event endpoint:

  1. serve.ts and web.ts — server.stop() is dead code (unreachable after await new Promise(() => {})), and no signal handlers exist. On SIGTERM/SIGINT, Instance.disposeAll() never runs, so MCP subprocesses, LSP servers, and other child resources are orphaned.

  2. server.ts — The SSE /event endpoint puts clearInterval(heartbeat) and unsub() inside stream.onAbort(), which Hono only calls on client disconnect. When the server closes the stream on InstanceDisposed via stream.close(), cleanup never runs and the interval + subscription leak.

Changes

serve.ts and web.ts — Add signal handlers following the existing pattern from worker.ts:137-147:

const shutdown = async () => {
  await Promise.race([Instance.disposeAll(), new Promise((resolve) => setTimeout(resolve, 5000))])
  server.stop(true)
  process.exit(0)
}
process.on("SIGTERM", shutdown)
process.on("SIGINT", shutdown)

server.ts — Add clearInterval(heartbeat) and unsub() to the InstanceDisposed handler before stream.close(). Both calls are idempotent, so the existing onAbort handler remains as a fallback for normal client disconnects.

How did you verify your code works?

  • Full test suite passes (996 tests, 0 failures)
  • Manual: bun dev serve, then kill -TERM <pid> — process now exits cleanly (exit 0) instead of being killed by signal (exit 143)
  • Code review: the shutdown pattern is identical to the existing worker.ts implementation that already handles this correctly

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cdenneen and LanKing reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
s2bomb added 2 commits February 18, 2026 21:50
@s2bomb
fix(opencode): add graceful shutdown to serve and web commands
ca331e5
server.stop() after await new Promise(() => {}) is unreachable dead code.
No signal handlers exist, so SIGTERM/SIGINT use the kernel default and
Instance.disposeAll() never runs — orphaning MCP subprocesses, LSP servers,
file watchers, and SQLite connections.

Add signal handlers following the existing pattern from worker.ts:137-147:
timeout-bounded Instance.disposeAll() + server.stop(true) + process.exit(0).
@s2bomb
fix(opencode): clean up SSE heartbeat on server-initiated stream close
dc72447
clearInterval(heartbeat) and unsub() only run inside stream.onAbort(),
which Hono only fires on client disconnect. When the server closes the
stream on InstanceDisposed via stream.close(), the interval and bus
subscription leak. Add cleanup before stream.close() in the
InstanceDisposed handler. Both calls are idempotent so the onAbort
fallback for normal client disconnects still works.
@s2bomb s2bomb mentioned this pull request Feb 18, 2026
Memory leak: serve and web commands leak child processes on exit; SSE heartbeat interval leaks on server-initiated close #14091
Closed
This was referenced Feb 26, 2026
fix(opencode): handle client disconnect in SSE stream writes #15147
Closed
fix(web): dispose idle MCP server instances to prevent process accumulation #15326
Closed
fix: prevent SSE stream memory leaks on client disconnect #15630
Closed
This was referenced Apr 9, 2026
fix(sdk): ensure opencode server closes cleanly via process groups #21626
Closed
fix(sse): prevent memory leak from zombie SSE connections #22550
Closed
@opencode-agent
opencode-agent Bot force-pushed the dev branch from 2f2ed11 to 9f201d6 Compare April 16, 2026 21:55
@github-actions

github-actions Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot closed this Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Memory leak: serve and web commands leak child processes on exit; SSE heartbeat interval leaks on server-initiated close

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@s2bomb
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.