Skip to content

feat(workers): report why a worker failed or a request was cancelled - #743

Merged
johnstonmatt merged 10 commits into
mainfrom
FUNC-921/report-cancellation-reason
Nov 26, 2026
Merged

johnstonmatt merged 10 commits into
mainfrom
FUNC-921/report-cancellation-reason

Conversation

@johnstonmatt

@johnstonmatt johnstonmatt commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

This fix lets platform operators judge the health of the service.

A request that fails for lack of a worker reports a single error no matter why it failed, so operators can't tell the function's own fault from the platform's. Two cases are affected:

  • Cancellation: every cancelled request reports WorkerRequestCancelled. A function that used up its own CPU, memory or wall-clock budget looks the same as a worker the supervisor reclaimed in the middle of a request.
  • Creation: InvalidWorkerCreation covers a module that threw during evaluation, and also two cases where no worker was ever produced: the pool dropped the sender, or the worker didn't come up in time.

Changes:

  • Cancellation:
    • Add WorkerExitStatus::Terminated(ShutdownReason) and WorkerExit::shutdown_reason().
    • The worker task records the shutdown reason from the Shutdown event it already observes on isolate exit.
    • A new cancellation_error helper in pool.rs picks the error:
      • An uncaught exception comes first.
      • CPUTime, Memory and WallClockTime give WorkerResourceExhausted.
      • EarlyDrop and TerminationRequested give WorkerReclaimed.
      • Otherwise it reports RequestCancelledBySupervisor, which surfaces as WorkerRequestCancelled.
  • Creation:
    • The pool sends a typed WorkerCreationTimeout instead of anyhow!("worker did not respond in time").
    • op_user_worker_create reports both no-worker cases as WorkerUnavailable, and the message still tells them apart.
    • InvalidWorkerCreation now means only that evaluating the module failed.
  • Registration:
    • WorkerResourceExhausted, WorkerReclaimed and WorkerUnavailable are registered in ext/runtime/js/errors.js and declared in types/global.d.ts.
    • deno_core turns an unregistered class into a plain Error, so without registration e.name would never match.
  • Request-path match:
    • The match lists WorkerCreationTimeout explicitly, mapped to InvalidWorkerResponse, rather than using a wildcard. It can't occur there, but listing it makes the compiler force a decision when someone adds the next variant.

Embedders

This is a breaking change for any embedder that maps these errors:

  • Three new op error codes: WorkerResourceExhausted, WorkerReclaimed and WorkerUnavailable. An embedder that doesn't know them sends those requests down its unhandled-error path, which typically means a generic 5xx and sometimes a logged exception per request.
  • InvalidWorkerCreation narrows. Code that keys on it for creation timeouts or unavailable workers stops seeing those cases.

An embedder has to handle the new names before it runs this runtime. Supabase's own embedder already handles all three.

Verification

  • Unit tests in pool.rs cover each shutdown reason, the fallback, and an uncaught exception winning over a recorded reason.
  • Two integration tests that asserted InvalidWorkerCreation: worker did not respond in time now expect WorkerUnavailable. The worker boot error assertions are unchanged because that case keeps its name.
  • CI: fmt, clippy, cargo test.

@johnstonmatt
johnstonmatt marked this pull request as ready for review September 22, 2026 04:00
A cancelled request was reported as WorkerRequestCancelled whatever the
cause, so a worker that used up its own CPU or memory budget and one the
host reclaimed mid-request were indistinguishable to the caller. An
embedder wanting to attribute the failure had nothing to go on but the
message text.

The supervisor already computes the cause and sends it to the event
worker as a ShutdownEvent, but it never reached the request path. The
worker task receives that same event when the isolate finishes, so it
now records the reason on WorkerExit alongside the uncaught-exception
status it already tracks, and the pool reads it when deciding how to
fail an in-flight request.

WorkerResourceExhausted covers CPUTime, Memory and WallClockTime.
WorkerReclaimed covers EarlyDrop and TerminationRequested. Anything
else, including a worker stopped before a reason was recorded, keeps
reporting WorkerRequestCancelled, so callers that only know the old name
are unaffected.
@johnstonmatt
johnstonmatt force-pushed the FUNC-921/report-cancellation-reason branch from c73a51f to 30f0f2e Compare September 22, 2026 04:07
…ed module

InvalidWorkerCreation covered three unrelated outcomes: the pool dropped
the sender, the worker did not come up before the pool gave up, and the
module threw while being evaluated. Only the last is the caller's own
code failing, and an embedder had no way to tell them apart.

The pool now sends a typed WorkerCreationTimeout instead of a message
built from a string, so the two cases where no worker was produced can be
matched on and reported as WorkerUnavailable. InvalidWorkerCreation keeps
its name and now means only that evaluating the module failed.

The request path lists the new variant explicitly rather than widening
its match to a wildcard, so a future variant still has to be decided
there. Two integration tests asserted the old creation-timeout message
and now expect the new name.
…d in JS

An op error whose class isn't registered reaches JS as a plain Error
("Unregistered error class"), so callers matching on e.name never saw
either name.
Without it the error reaches JS as a plain Error ("Unregistered error
class"), which is why req_failure_case_timeout never saw the new name.
A request cancelled while its worker was running still failed with the
unqualified WorkerRequestCancelled, because send_user_worker_request
built that error itself instead of using cancellation_error. The
shutdown reason was also recorded only after the worker loop ended,
later than the cancelled requests read it.

The supervisor now records the reason before it cancels the requests,
without overwriting an uncaught exception, and every cancellation site
uses cancellation_error. The CPU, memory and wall-clock integration
tests now expect WorkerResourceExhausted, and unit tests cover the
reclaimed case and the precedence of an uncaught exception.
@johnstonmatt
johnstonmatt requested a review from a team November 24, 2026 03:26
feat(workers): report a failed worker creation separately from a failed module
@johnstonmatt johnstonmatt changed the title feat(edge-runtime/workers): report why the supervisor cancelled a request feat(workers): report why a worker failed or a request was cancelled Nov 26, 2026
@johnstonmatt
johnstonmatt merged commit 279f916 into main Nov 26, 2026
5 checks passed
@johnstonmatt
johnstonmatt deleted the FUNC-921/report-cancellation-reason branch November 26, 2026 09:11
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