Skip to content

fix: intermittent first-request stall on Bun serve under CPU load - #46436

Closed
aman-kumar-gw wants to merge 1 commit into
anomalyco:devfrom
aman-kumar-gw:fix/bun-first-request-stall
Closed

aman-kumar-gw wants to merge 1 commit into
anomalyco:devfrom
aman-kumar-gw:fix/bun-first-request-stall

Conversation

@aman-kumar-gw

@aman-kumar-gw aman-kumar-gw commented Aug 31, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #46437

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

On Bun, opencode serve intermittently hangs its first request for tens of seconds under CPU load, then recovers (startup-only). I traced it end to end: the request-handler fiber is never forked — the TCP connection is accepted and the request bytes are read into userspace (rx_queue=0), but Bun's node:http never emits the JS request event while the loop is parked in epoll_pwait2, so Effect never sees the request. It's a Bun regression: 1.3.11 doesn't stall, 1.3.12/1.3.13/1.3.14 do — setImmediate-style work gets starved under load.

Effect's default MixedScheduler flushes fiber dispatch through globalThis.setImmediate, which hits the same starvation. This routes that flush through a MessageChannel macrotask instead (with a setImmediate/setTimeout fallback) — a different event-loop scheduling path than re-entering setImmediate, which keeps the loop turning over so Bun's pending node:http dispatch runs. Ports are unref'd so short-lived CLI runs still exit (the default scheduler is process-wide, not just serve). Delivered through the existing patches/[email protected].

How did you verify your code works?

Cold-cycled opencode serve on a 2-vCPU container under CPU contention and timed the first request (>8s counted as a stall):

  • unpatched (Bun 1.3.14): ~100% stall (6/6, 40s timeouts)
  • Bun 1.3.11 downgrade only: 2/40 (~5%)
  • this patch: 1/340 (~0.3%), non-stalling requests < 1s

Screenshots / recordings

N/A (not a UI change).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…heduler flush)

Under CPU load, `opencode serve` on Bun intermittently hangs the first request
for tens of seconds. The root cause is a Bun node:http dispatch starvation: the
connection is accepted and read natively but the JS `request` event is never
emitted while `setImmediate`-style work is starved in the event loop.

Effect's default scheduler flushes fiber dispatch through `globalThis.setImmediate`,
which hits the same starvation. Route the flush through a MessageChannel macrotask
(with a setImmediate/setTimeout fallback) via the existing effect patch. Ports are
unref'd so short-lived CLI processes still exit. Cuts the stall rate ~100% -> ~0.3%
under heavy contention.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:title labels Aug 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Hey! Your PR title Fix intermittent multi-second first-request stall on Bun (node:http dispatch starvation) doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@aman-kumar-gw aman-kumar-gw changed the title Fix intermittent multi-second first-request stall on Bun (node:http dispatch starvation) fix: intermittent first-request stall on Bun serve under CPU load Aug 31, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Aug 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intermittent multi-second first-request stall on Bun serve under CPU load (node:http dispatch starvation)

1 participant