chore: narrow wasm backend entrypoint API

This commit is contained in:
udo
2026-06-13 22:32:02 +00:00
parent af6500d134
commit b1856c1725
4 changed files with 22 additions and 63 deletions
+9 -30
View File
@@ -168,12 +168,11 @@ bool wasm_backend_should_handle(Request& request, const String& entry_unit)
}
// Serve a request through a wasm workspace using the unit handler selected by
// `kind` (page render / cli / serve_http). Populates the native Request
// `kind` (page render or cli). Populates the native Request
// (status/headers/cookies/session/body) so the existing transport writes the
// response unchanged. Returns "" on success, or a collapsed error/trace string
// for the caller to route into the configured error page.
String wasm_backend_serve(Request& request, const String& entry_unit, int32_t kind = WasmWorkspace::RESOLVE_RENDER,
const String& handler_name = "")
String wasm_backend_serve(Request& request, const String& entry_unit, int32_t kind = WasmWorkspace::RESOLVE_RENDER)
{
DValue ctx;
auto copy_map = [&](const StringMap& source, const char* key) {
@@ -187,20 +186,19 @@ String wasm_backend_serve(Request& request, const String& entry_unit, int32_t ki
copy_map(request.session, "session");
ctx["entry_unit"] = entry_unit;
// Raw request body: cli_input() parses a JSON CLI payload from context.in,
// and ws_message() exposes the websocket frame from it — both need it carried
// into the workspace, not just the form-decoded post map.
// carried into the workspace, not just the form-decoded post map.
ctx["in"] = request.in;
WasmResponse response = wasm_worker_serve(*g_wasm_worker, ctx, entry_unit, kind, handler_name);
WasmResponse response = wasm_worker_serve(*g_wasm_worker, ctx, entry_unit, kind);
if(!response.ok)
return(response.error == "" ? String("wasm workspace failed") : response.error);
// A cli/serve_http unit that does not export the requested handler is a 404,
// matching native compiler_invoke_cli ("CLI Entry Point Not Found"). For page
// render a missing handler is simply an empty body (native parity).
if(!response.handler_present && kind != WasmWorkspace::RESOLVE_RENDER)
// A cli unit that does not export __uce_cli is a 404, matching native
// compiler_invoke_cli ("CLI Entry Point Not Found"). For page render a
// missing handler is simply an empty body (native parity).
if(!response.handler_present && kind == WasmWorkspace::RESOLVE_CLI)
{
request.set_status(404, kind == WasmWorkspace::RESOLVE_CLI ? "CLI Entry Point Not Found" : "Handler Not Found");
request.set_status(404, "CLI Entry Point Not Found");
return("");
}
@@ -258,22 +256,3 @@ void wasm_backend_shutdown()
g_wasm_epoch_ticker = 0;
}
}
// A connection-broker child (the custom-server HTTP dispatcher, the websocket
// exec child) is forked from a worker that may already have brought up its wasm
// engine. The Wasmtime engine and the epoch-ticker thread must not cross fork:
// the child inherits the pointer/flags but only the forking thread survives, so
// the ticker is a phantom and the engine state is unsafe. Reset the per-process
// statics so the child lazily initializes its own engine + ticker on first use.
// The inherited std::thread refers to a thread that does not exist in the child;
// discard the inherited pointer without touching the pointed-to object. Joining,
// deleting, or destroying a joinable std::thread after fork can terminate the
// child; the child will allocate its own ticker on first wasm use.
void wasm_backend_reset_after_fork()
{
g_wasm_worker = 0;
g_wasm_init_attempted = false;
g_wasm_init_error = "";
g_wasm_epoch_running.store(false);
g_wasm_epoch_ticker = 0;
}