wasm_worker_error (wasm v0.16.0)

View Source

The worker's own error type, and the closed set of kinds it can carry.

The calls into a running wasm_script_worker (run, submit, await, cancel) answer with one of these or with a result, and do not raise: a dead worker, an exhausted limit and a guest that trapped are all values. That is the runtime's promise, Failures are values at the guest boundary, applied to the layer above it. wasm_script_worker:stop/1 on a worker that has already exited still exits, as any gen_server does.

Why this is not a wasm_error

wasm_error:class/0 is malformed | invalid | link | trap | exhaustion, and none of those describes a request that ran out of wall clock or a caller that cancelled. Inventing a sixth class would put worker vocabulary in the runtime's namespace, where a future runtime change would have to keep it working. So the worker has its own type and the runtime's errors travel inside it untouched:

#{class => runtime, kind => runtime_failure, msg => ~"trap",
  ctx => #{error => WasmError}}

The runtime's own class and kind are still there, in ctx.error, where they can grow without anything here changing.

The kind is always an atom this module names

kind/0 below is the whole set. That matters because of the other half of the rule: nothing a guest supplies becomes an atom. The atom table is node-wide and never reclaimed, so a language's own vocabulary travels as a binary in ctx, never as a kind:

#{class => adapter, kind => adapter_failure, msg => ~"main is not defined",
  ctx => #{code => ~"no_entry_point"}}

adapter_failure is the kind; ~"no_entry_point" is the profile's code. A profile can add codes for ever and this set does not move.

Building one

wasm_worker_error:worker(timeout, ~"deadline reached", #{after_ms => 5000}),
wasm_worker_error:adapter(exit, ~"non-zero exit", #{code => 2}),
wasm_worker_error:runtime(WasmError).

Summary

Types

Which layer decided the request had failed.

Every kind the worker can produce. Closed, and checked by is_error/1.

What every worker entry point answers with when it does not answer ok.

Functions

An error the adapter diagnosed.

Whether a term is a well-formed worker error.

Every kind this module can produce.

Wrap a runtime error without reinterpreting it.

An error the kernel itself decided on.

Types

class()

-type class() :: worker | adapter | runtime.

Which layer decided the request had failed.

worker is the kernel's own judgement, adapter is the adapter's, and runtime means the runtime returned an error and nobody reinterpreted it.

kind()

-type kind() ::
          timeout | cancelled | output_limit | result_limit | crashed | insufficient_limit |
          bad_stage_path | busy | no_reaper | cleanup_saturated | still_running | already_awaited |
          unknown_ref | no_worker | worker_died | exit | adapter_failure | runtime_failure.

Every kind the worker can produce. Closed, and checked by is_error/1.

A guest or an adapter that needs a vocabulary of its own puts a binary in ctx, because this set must not grow with the number of languages.

worker_error()

-type worker_error() :: #{class := class(), kind := kind(), msg := binary(), ctx := map()}.

What every worker entry point answers with when it does not answer ok.

Functions

adapter(Kind, Msg, Ctx)

-spec adapter(exit | adapter_failure, binary(), map()) -> worker_error().

An error the adapter diagnosed.

Only two kinds are available, exit and adapter_failure, and that is deliberate: an adapter with more to say says it as a binary code in ctx.

class_of/1

-spec class_of(worker_error()) -> class().

is_error/1

-spec is_error(term()) -> boolean().

Whether a term is a well-formed worker error.

All four keys, a known class, a kind from kinds/0, a binary message and a map context. The kit asserts this on every error every adapter produces, which is what makes the error model a contract rather than a convention.

kind_of/1

-spec kind_of(worker_error()) -> kind().

kinds()

-spec kinds() -> [kind()].

Every kind this module can produce.

Exported so the conformance kit can assert that an adapter did not invent one, which is the check that keeps the set closed.

runtime(Err)

-spec runtime(wasm_error:error()) -> worker_error().

Wrap a runtime error without reinterpreting it.

The kind is always runtime_failure. The runtime's own class and kind stay in ctx.error, so a trap, an exhausted fuel budget and a memory refusal are told apart by reading that rather than by this set growing three more atoms.

runtime(Err, Ctx)

-spec runtime(wasm_error:error(), map()) -> worker_error().

worker(Kind, Msg, Ctx)

-spec worker(kind(), binary(), map()) -> worker_error().

An error the kernel itself decided on.