Skip to content

feat: add allowWorkerIdleExit option to suppress idle-worker error events#133

Open
trafgals wants to merge 1 commit into
tinylibs:mainfrom
trafgals:feat/allow-worker-idle-exit
Open

feat: add allowWorkerIdleExit option to suppress idle-worker error events#133
trafgals wants to merge 1 commit into
tinylibs:mainfrom
trafgals:feat/allow-worker-idle-exit

Conversation

@trafgals

Copy link
Copy Markdown

What

Tinypool currently emits an error event on the public interface whenever a worker exits while it has no in-flight task. The intent is to surface post-task uncaught exceptions (e.g. an async setTimeout(() => { throw ... }, 0) that lands after pool.run() has returned), and that behavior is locked in by test/uncaught-exception-from-handler.test.ts.

The same code path also fires for workers that die from external causes that the consuming pool already has visibility into — most concretely, workerd subprocesses that segfault during teardown (see cloudflare/workerd#6763, also discussed in this comment thread on the upstream workerd issue tracker). When the workerd child dies after a vitest test file has reported its results, cloudflare/vitest-pool-workers is unable to suppress the resulting Worker exited unexpectedly event, and the whole vitest run exits with code 1 even though every test body was already reported as passing.

This PR adds an opt-in allowWorkerIdleExit: boolean option (default false) that lets consuming pools opt out of the public error emission for the idle-worker case. Workers that exit mid-task still surface the error to the in-flight task's callback, so genuine task failures continue to fail the affected task.

How

  • Options.allowWorkerIdleExit?: boolean (default false)
  • FilledOptions.allowWorkerIdleExit: boolean (the runtime-resolved value)
  • kDefaultOptions.allowWorkerIdleExit = false (preserves current behavior)
  • The existing worker 'error' handler is now a 3-branch conditional:
    • task in flight → reject the task's callback (unchanged)
    • task not in flight, opt-out enabled → silent (no error event)
    • task not in flight, opt-out disabled → emit error event (unchanged)

Tests

  • New test/allow-worker-idle-exit.test.ts runs the same fixture twice — once with the option unset (asserting the existing error event) and once with allowWorkerIdleExit: true (asserting the error event is suppressed).
  • The existing test/uncaught-exception-from-handler.test.ts continues to lock in the default behavior.

Cross-references

  • cloudflare/workerd#6763 — upstream workerd SIGSEGV
  • cloudflare/vitest-pool-workers PR #14793 — companion change that handles the same scenario at the pool layer for the bundled 0.16.x version.

Why not just patch vitest-pool-workers?

That pool does include a mitigation (PR #14793), but it has to live in their vendored copy of tinypool (dist/worker/lib/tinypool.mjs) until/unless this option exists upstream. Surfacing it here lets the fix land once and apply to every pool that wants to suppress idle-worker noise.

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

…ents

When a worker exits while it has no in-flight task, tinypool historically
emits an `error` event on the public interface. This is useful for
catching uncaught exceptions that fire after the task's synchronous
return — but it also fires for workers that die from external causes
(e.g. a child process crashing, a workerd subprocess segfaulting during
teardown — see cloudflare/workerd#6763),
which is a class of noise that the consuming pool often already has
visibility into.

Add an opt-in `allowWorkerIdleExit: boolean` option (default `false`).
When `true`, the pool does not surface an `error` event for workers
that exit while idle. Workers that exit mid-task still report the error
to the in-flight task's callback, preserving the existing behavior for
genuine task failures.

A test pairs both modes against the same fixture so the regression for
the default behavior is locked in alongside the new opt-in path.

@AriPerkkio AriPerkkio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Vitest no longer uses tinypool. Please try to reproduce the issue with vitest@4, and open a bug report on Vitest repository with a fix.

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