Skip to content

fix(runtime-quickjs): offload QuickJS executions to worker threads in selfhost to prevent event loop blocking - #1852

Closed
Adityakk9031 wants to merge 2 commits into
UsefulSoftwareCo:mainfrom
Adityakk9031:fix/selfhost-quickjs-worker-thread
Closed

fix(runtime-quickjs): offload QuickJS executions to worker threads in selfhost to prevent event loop blocking#1852
Adityakk9031 wants to merge 2 commits into
UsefulSoftwareCo:mainfrom
Adityakk9031:fix/selfhost-quickjs-worker-thread

Conversation

@Adityakk9031

Copy link
Copy Markdown
Contributor

Fixes #1616

Summary

Self-host executions in @executor-js/runtime-quickjs previously ran QuickJS synchronously on the Node.js/Bun server's main thread. When a CPU-bound execution was run (e.g. while (true) {}), it monopolized the main event loop until hitting timeoutMs (up to 5 minutes by default), starving health probes, OAuth sessions, web UI, and concurrent MCP requests.

This PR introduces optional worker thread execution to @executor-js/runtime-quickjs and enables it for self-hosted instances:

  1. Worker Thread Execution (worker: true): Added worker?: boolean option to QuickJsExecutorOptions. When enabled, makeQuickJsExecutor delegates execution to a dedicated node:worker_threads.Worker running packages/kernel/runtime-quickjs/src/worker.ts.
  2. Bidirectional IPC: Tool invocations inside the QuickJS worker seamlessly bridge back to the parent thread's toolInvoker and yield results/errors back to the worker context.
  3. Cancellation & Preemption: Interruption or timeout terminates the worker thread cleanly without hanging or stalling the host server's main event loop.
  4. Self-Host Enabled: SelfHostCodeExecutorProvider in apps/host-selfhost/src/execution.ts sets worker: true.

Verification

  • Unit tests added to packages/kernel/runtime-quickjs/src/index.test.ts covering plain executions, bidirectional tool dispatches, tool error propagation, and CPU-bound loop preemption in worker threads.
  • Vitest tests:
    • packages/kernel/runtime-quickjs: 32/32 tests passed (including all worker thread suites).
    • apps/host-selfhost: 137/137 tests across 24 test files passed.
  • Gates:
    • bun run typecheck: 44/44 successful.
    • bun run lint: 0 warnings, 0 errors.
    • bun run format:check: All modified files formatted and verified.

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.

Self-host: a CPU-bound execute blocks the whole server for up to 5 min (QuickJS runs on the main thread)

2 participants