Skip to content

fix(rpc): reject pending calls when target disconnects - #34974

Closed
HEETMEHTA18 wants to merge 4 commits into
anomalyco:devfrom
HEETMEHTA18:fix-rpc-hang
Closed

fix(rpc): reject pending calls when target disconnects#34974
HEETMEHTA18 wants to merge 4 commits into
anomalyco:devfrom
HEETMEHTA18:fix-rpc-hang

Conversation

@HEETMEHTA18

@HEETMEHTA18 HEETMEHTA18 commented Jul 2, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #34981

Type of change

  • Bug fix

What does this PR do?

When a Worker emits error (uncaught exception) or messageerror, all pending RPC call() promises hung forever because no listener existed to reject them.

Changes:

  • Store { resolve, reject } in pending map instead of just resolve
  • Add error and messageerror event listeners on the target
  • Add closed flag — calls made after disconnect reject immediately instead of hanging again
  • Add try/catch around postMessage for DataCloneError

Verified with a real Bun Worker that throws at import time:

  • Worker uncaught throw fires error event → rejectAll() runs
  • Calls made after closed reject immediately with "RPC target disconnected"
  • terminate() does NOT fire events in Bun — that case is handled by caller-side timeouts

How did you verify your code works?

$ bun test test/rpc-hang.test.ts
  ✓ rejects pending calls when target disconnects (error event)
  ✓ rejects pending calls on messageerror
  ✓ rejects calls made after target already disconnected
  ✓ resolves normally when target responds
  ✓ rejects when a real Bun Worker throws an uncaught error
  5 pass, 0 fail

Also verified: terminate() in Bun fires no events (see test/worker-events.test.ts), so this fix covers uncaught throws which do fire error.

Typecheck: 29/29 packages pass.

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added needs:issue and removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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

When the target Worker emits 'error' (uncaught exception) or
'messageerror', all pending RPC call() promises hung forever.

- Store { resolve, reject } in pending map (was just resolve)
- Add 'error' and 'messageerror' event listeners on target
- Add closed flag to reject calls made after disconnect
- Add try/catch around postMessage for DataCloneError
- Guard against post-disconnect calls returning new hanging promises

Verified against a real Bun Worker that throws at import time:
  - Worker uncaught throw fires 'error' event -> rejectAll runs
  - Calls made after 'closed' reject immediately
@HEETMEHTA18

Copy link
Copy Markdown
Author

Updated the branch based on review feedback:

Changes made:

  1. Closed flag — Calls made after the target has already disconnected now reject immediately instead of hanging forever in a new Promise.

  2. Real Worker test — Added test/fixture/crashing-worker.ts and a test that creates a real new Worker() that throws at import time. Confirmed Bun fires "error" on uncaught exceptions, which triggers rejectAll().

  3. Investigated terminate() — Bun does NOT fire any events when worker.terminate() is called. This is a Bun/platform limitation, not something rpc.ts can detect. The caller in tui.ts already handles this with a 5s timeout + stopped flag, so no regression for the actual use case.

Test results:

bun test test/rpc-hang.test.ts
  ✓ rejects pending calls when target disconnects (error event)
  ✓ rejects pending calls on messageerror
  ✓ rejects calls made after target already disconnected
  ✓ resolves normally when target responds
  ✓ rejects when a real Bun Worker throws an uncaught error
  5 pass, 0 fail

Typecheck: 29/29 packages pass.

@github-actions

github-actions Bot commented Aug 2, 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.

bug(rpc): pending RPC calls hang forever when target Worker disconnects

1 participant