wasm_cleanup_steward (wasm v0.16.0)

View Source

Internal: the per-request cleanup steward.

One steward owns the cleanup interaction with the reaper for a single request, so the guardian talks to the steward and never to the reaper directly. The full protocol -- the ledger, the send_request transport, the state machine and adoption -- is described in test/audit/CLEANUP_STEWARD.md.

The steward is the operation authority. It assigns each operation a monotonic sequence, keeps a ledger of Sequence => {Operation, pending | {done, Result}}, and carries operations to the reaper with gen_server:send_request/2, matching answers with gen_server:check_response/3 so it never blocks. Each operation's OperationId is {RequestId, Sequence}; reserve is sequence 0 and stays synchronous, because it runs at setup before the deadline that matters.

It pins the exact reaper pid it reserved against and monitors it. While that reaper lives, operations go to it. When it dies, the operations it was still answering stay pending: a replacement reaper's adopt_request, sent during its sweep, re-pins the steward, which hands over the ledger, mirror and next sequence in adopt_reply and then resends the pending operations on the same ordered path, so none races ahead of the sequence handover and a request survives a reaper restart with its sequence and volatile state intact. Only when an operation must be sent with no reaper pinned does the steward ask the cleanup manager whether one can be reached; a definitive gone fails the operation rather than holding it, so a request whose reaper will not return is not stuck.

Summary

Functions

Tell the steward the request is done and it should finish it with the reaper.

Forward a cleanup operation to the reaper without waiting for it.

Claim the request's cleanup capacity and directory, via the reaper.

Stop a steward. Non-blocking, so a caller tearing a request down never waits on a steward that is itself waiting on the reaper.

Types

operation()

-type operation() ::
          {register, wasm_worker_adapter:action()} |
          {withdraw, wasm_worker_adapter:token()} |
          {transfer, module(), term()}.

Functions

complete(Steward, Guardian)

-spec complete(pid(), pid()) -> ok.

Tell the steward the request is done and it should finish it with the reaper.

The steward submits finish, which asks the reaper to own cleanup, and answers Guardian with {cleanup_owned, Steward} once the reaper accepts it, or {cleanup_unavailable, Steward} if no reaper can be reached, so the guardian either exits or falls back to its mirror.

forward(Steward, CorrRef, ReplyTo, Operation)

-spec forward(pid(), reference(), pid(), operation()) -> ok.

Forward a cleanup operation to the reaper without waiting for it.

Returns at once. The steward sends the operation to the reaper and, when the answer arrives, sends {steward_reply, CorrRef, Reply} to ReplyTo, so the guardian correlates the answer to the runner it is holding and never blocks on the reaper itself.

handle_call/3

handle_cast/2

handle_continue/2

handle_info/2

init/1

reserve(Steward, Owner, Root, RelPath)

-spec reserve(pid(), pid(), wasm_worker_adapter:root_id(), binary()) ->
                 {ok, file:filename_all()} | {error, wasm_worker_error:worker_error()}.

Claim the request's cleanup capacity and directory, via the reaper.

Synchronous, and it stays that way: it runs at setup, before the runner and before the request deadline is being enforced, so the caller waiting on it is not the caller that owns a deadline. It also pins the reaper this steward talks to from here on.

start_link(RequestId)

-spec start_link(wasm_worker_reaper:request_id()) -> {ok, pid()}.

stop(Steward)

-spec stop(pid()) -> ok.

Stop a steward. Non-blocking, so a caller tearing a request down never waits on a steward that is itself waiting on the reaper.