Skip to content

server: engine_module, the ServiceModule counterpart (host-pool slice 2) - #135

Open
joelteply wants to merge 7 commits into
fable/frames-s4from
fable/engine-module-s4
Open

joelteply wants to merge 7 commits into
fable/frames-s4from
fable/engine-module-s4

Conversation

@joelteply

Copy link
Copy Markdown

Slice 2 of the unmerged fable/host-pool work, stacked on #134. This is the job-system pattern every engine module will use (Joel, 2026-11-16: "build a servicemodule/runtimemodule equivalent base class there too").

What it adds (header and test only, no call sites yet)

engine_module<Snapshot>:

  • Its own thread: a std::jthread, stopped by its stop token.
  • Jobs as tickets: move-only jobs come back through a completion ring, not futures or promises.
  • Wakes only on work or stop: no polling, no timer.
  • Snapshots that never wait: it publishes an immutable snapshot, and readers copy it without waiting on its work.
  • Typed refusals: std::expected<..., module_refusal>.
  • A clean stop: queued jobs are cancelled with a typed reason and their captures released.
  • Enforced destruction order: a module that skips stop_and_join() aborts loudly.

The test needs C++23 (std::expected, jthread). The server target moves to C++23 in the slice that wires the first module; that also supersedes #129's bump to C++20.

Measured (M5, Apple clang 21, 2026-11-16)

  • test-server-module: ok.
  • Standalone at -std=c++23 -Wall -Wextra -Werror under ThreadSanitizer: ok, 0 reports.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…le (host-pool slice 2)

From fable/host-pool (3cf8a4e, b1d78ee, ae8b419; Codex's reviews folded in), as its final form:
an engine module owns its thread (std::jthread, stop by token), takes move-only jobs as tickets with a
completion ring (a job system, not a promise library), wakes only on work or stop, publishes an immutable
snapshot readers copy without waiting on its work, refuses typed (std::expected<..., module_refusal>),
and a module's stop cancels queued jobs typed and releases their captures. No call sites yet: the server
target moves to C++23 in the slice that wires the first module; only this test needs it now.

Receipt (M5, Apple clang 21): test-server-module ok from the cmake build, and standalone at -std=c++23
-Wall -Wextra -Werror under -fsanitize=thread: ok, 0 reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Author

Review exact5e5505e5ca2f276922efd7c826c100413dd75296: BLOCK pending job/completion contract corrections.

  1. run invokes a std::function without exception handling or a no-throw enforced callable contract. A throwing job terminates the process from the worker; queued cancellation callbacks can likewise throw before later tickets become terminal. Define typed failed terminal results through the existing completion owner and guarantee remaining captures/tickets settle, or enforce a genuinely nonthrowing job API. Add run/cancel fault tests.
  2. The promised move-only/allocation-bounded job system currently uses std::function<void(job_state)>, requiring copy-constructible targets and potentially heap allocating per submission. Snapshot publish also allocates shared state. This does not establish the4.7.12 owner-preallocated job/result contract. Use the intended owner-owned storage/tickets and prove bounded allocations, or narrow this slice's claim and do not wire it into the hot path.
  3. snapshot_cell::read takes a mutex: “snapshots never wait” is not the implemented guarantee. It avoids waiting on job execution, but can contend with publication/other readers. Describe that accurately and supply read/publish contention evidence before treating it as the no-wait primitive.

Stack remains dependent on blocked134. No build or runtime failure reproduction performed; findings are source-level contracts.

joelteply and others added 3 commits November 16, 2026 13:23
… typed fault, snapshots read with no lock (Codex's review of #135)

- inplace_job<Bytes, Args...>: a move-only callable stored INLINE in the ring (a fixed capture size, checked
  at compile time; nothrow-move; never a heap allocation, never a copyable target). std::function is gone.
- A run or cancel call that throws is contained: recorded as module_fault {ticket, during, what} and counted
  in stats.faulted; its ticket still completes and the worker lives on (it terminated before).
- snapshot_cell is a seqlock over atomic words: the writer never waits; a reader copies by value and retries
  only when a publish overlapped its copy; Snapshot must be trivially copyable (counters and ids), enforced.
  snapshot() returns the value, not a shared_ptr behind a mutex.
- cancel_queued takes one job at a time with no allocation and no lock held across a call.

Tests (one regression each): a throwing job is a typed fault, the next job runs, the faulted ticket completes;
a throwing cancel during stop is typed and no waiter hangs; a job owning a unique_ptr queues, runs once and
releases it; a reader racing 200,000 publishes never sees a torn value. Receipt (M5, Apple clang 21):
test-server-module ok; -std=c++23 -Wall -Wextra -Werror under -fsanitize=thread 3 of 3 ok, under
-fsanitize=address,undefined ok.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Author

Re-review exactaec424d36a791a5bf071257bddb66461d8ada7b2: inline move-only capture and contained exceptions improve prior findings, but BLOCK remains.

  1. submit accepts a default/moved-from empty job; inplace_job::operator() dereferences ops_ unconditionally. This is a null-pointer crash, not a catchable exception. Reject empty jobs with typed refusal before assigning a ticket; test default and moved-from submission.
  2. Fault handling stores only last_fault. Two jobs that throw overwrite the first ticket's failure, while completed advances normally. A consumer waiting for its ticket cannot reliably determine its terminal outcome, and its owner-owned result may remain unwritten. Publish each job's typed terminal status to its existing owner storage/completion slot (including cancellation failure), retain it until consumed. A global last-error record is diagnostic, not per-job completion truth.
  3. record_fault allocates std::string from e.what inside exception handling, which can itself throw (including bad_alloc) and terminate the worker. Keep required failure bookkeeping bounded/nonthrowing and typed; render diagnostic text only at export.

The snapshot implementation is lock-free in form but retries/spins while publication is odd; it is not a bounded/no-wait read if writer is descheduled. State this limitation and measure contention rather than claim it never waits. No runtime reproduction performed; source-level review.

joelteply and others added 2 commits November 16, 2026 13:42
…e, no string; empty jobs refused; the seqlock is described as it is (Codex's re-review of #135; Joel: strings, allocation and locking are the enemy)

- job_outcome {pending, ran, run_faulted, cancelled, cancel_faulted}: submit(job, &outcome) takes the owner's
  storage and the module writes how the job ended there. Nothing is lost when several jobs fault, and the
  catch allocates nothing and cannot throw (module_fault and its std::string are gone; job_outcome_name
  renders text only at an export edge). stats.faulted is an atomic counter.
- submit refuses an empty or moved-from job (module_refusal::empty): it is never queued or called.
- snapshot_cell: reads take no lock but are NOT wait-free; a writer descheduled mid-publish stalls readers,
  who yield after a few retries instead of spinning. Said so in the header.
- stopping(): owners (and tests) observe a requested stop.

Tests: two faulting jobs each keep their own run_faulted while the next job runs; a throwing cancel during stop
ends cancel_faulted and no waiter hangs (the blocker is released only after the stop is observed, so the run
is deterministic); an empty and a moved-from job are refused typed while the moved-to job runs. Receipt (M5,
Apple clang 21): -std=c++23 -Wall -Wextra -Werror under -fsanitize=thread 5 of 5 ok and under
-fsanitize=address,undefined 5 of 5 ok.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Author

Independent identity_crypto_review sourceAPPROVE revisedoutcomehandling exact478fe7c8dce518cf0784b86beb440e7f124d2205 conditional exactCI. Eachacceptedjob runs/cancelsonce, typedfaultoutcomes/capturedestruction/ticketcompletionordered; nointernalcatchallocation; inlinecapture/emptyrefusal sound. Caller mustretainoutcome/results untilticketcompletion (terminaloutcomealone precedescapturedestroy), distinctstorageperconcurrentjob, serializedshutdown. Snapshotreadretry limitationdocumented; test reads>0 needsstartupbarrier toavoid schedulingflakiness. UbuntuCIred stillblocksmerge; no builds.

…mit), not the C++23 library type the grid lacks (Codex's CI read of #135)

Ubuntu 22.04's libstdc++ has no <expected> (both ubuntu jobs failed at server-module.h). submit returns
module_submit {ticket, refusal}: queued under a ticket (from 1), or refused (ticket 0) with the typed reason.
No look-alike of the library type, no fallback: a plain struct this API owns. The test builds at C++20 (the
server's standard).

Also from the re-review: the outcome storage must stay valid until the owner observes the ticket's completion
(said in submit's contract); the torn-read test starts the writer only once the reader runs, so it always
overlaps; the faulting test jobs throw only on run (no noreturn warnings).

Receipt (M5, Apple clang 21): cmake build 0 warnings, test-server-module ok; -std=c++20 -Wall -Wextra -Werror
under -fsanitize=thread 3 of 3 ok and under -fsanitize=address,undefined 3 of 3 ok.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant