Repository navigation
fix(actions): run execute_script in a memory-bounded worker process (WI-2360) - #565
stefan-ernst wants to merge 3 commits into
Conversation
…WI-2360) Sobek ran action scripts inside the API process with no per-script heap bound, so any action.manage holder could save a script that allocates without bound and drive the whole instance into the memory-limit GC death spiral. Move execute_script into a dedicated worker process per execution, selected by WINDSHIFT_SCRIPT_WORKER=1 on the same binary. The worker owns a fresh Sobek realm and nothing else; the parent keeps token minting, route policy, the call budget, the write-ahead marker, and v2 dispatch, so the REST bridge semantics are unchanged. Budgets: - script_memory_mb (operator control, default 128) is the per-script heap budget. The worker sets debug.SetMemoryLimit and an in-worker monitor interrupts a runaway allocation; on Linux the parent also kills the worker when its resident set passes its baseline plus the budget. - script_timeout_ms and the node timeout still cap wall clock; the worker outlives the run deadline by a short grace so it can report its own failure. - concurrent_scripts still gates the parent, and the call and output budgets are unchanged. A killed or over-budget worker fails the node with a bounded, explainable error and leaves the API process unaffected. Condition scripts stay in-process; they are admin-gated and capped by script_timeout_ms and the 10 KiB source limit.
The parent RSS backstop now allows the worker's non-heap runtime overhead on top of the heap budget, so the worker's own heap monitor fires first for a script that is merely at its budget.
|
Performance review: I’d keep per-run process isolation, but change which executable gets launched. On Linux/arm64, this PR adds about 43 ms per
Startup dominates the small requests. A separate Go init trace counted 482 package initializers before the worker ran: A trivial node used 53 ms of CPU across parent and child, so 20 nodes/s would consume about one core. With four concurrent callers and a four-CPU quota, the PR sustained 74 trivial nodes/s at 67 ms p95. Four live workers paused at their first GET used 75 MiB of private memory combined, excluding shared executable pages. Three changes I’d request before merge:
Method: |
Address the PR review: - The parent now rejects a worker frame larger than 1 MiB before decoding it, so script-controlled method and path input cannot make the API process allocate. - An unset concurrent_scripts limit now resolves to a finite default of 4 before a worker is spawned, so process count is bounded even without operator configuration. - The timeout is one absolute instant computed before launch and shared with the worker, so startup time counts against the budget instead of granting a fresh timeout after initialization.
|
Thanks — all three are addressed in 1. Bound worker frames before decoding. 2. Finite default concurrency. 3. One absolute deadline. On the 43 ms/node: I agree the full binary is the wrong host for the worker. The three fixes above don't change spawn cost, so the minimal single-use worker binary is the right follow-up. Concretely: move the worker runtime plus the pure Sobek helpers ( I can fold that into this PR or land it separately — tell me which you prefer. The per-run process model and the launcher interface already accommodate it, so it's additive. |
|
Follow-up: retracting the separate minimal worker binary from my previous comment. It would break the single-binary deployment (one artifact, one entrypoint, That leaves the init cost as a real constraint, because a single binary always runs every linked package's
Option 2 is the only way I see to get the throughput back without a second artifact, and the item sanctions it. It is a meaningful change to the launcher (checkout/checkin, health check, recycling), so I'd rather not fold it in silently. Preference? If you want the pool I'll implement it on top of this PR; otherwise I'll leave the strict per-execution model as-is. |
|
Dropping this PR to keep the current one binary design. This can be picked up again as a micro binary spawn implementation but that would change the deployment surface |
Problem
execute_scriptran author-supplied JavaScript in Sobek inside the APIprocess. CPU was bounded (
vm.Interrupt, 30 s cap) and source capped at10 KB, but nothing bounded allocation. Any holder of
action.manage— everyauthenticated user in their personal workspace — could save and trigger a
script that allocates without bound and drive the whole instance into the
debug.SetMemoryLimitGC death spiral. Availability impact across tenants.This implements the item's option 1: move script execution out of the API
process.
Design
execute_scriptnow runs in a dedicated worker process per execution,selected by
WINDSHIFT_SCRIPT_WORKER=1on the same binary (handled at the topof
main.go). The worker owns a fresh Sobek realm and nothing else.The parent keeps everything security-relevant, so the REST bridge is
unchanged:
MintActionScriptCredential), route policy(
ValidateScriptAPIRequest), the call budget, the write-ahead marker, andInProcessV2Dispatcherall stay in the parent;api_request/consolemessages over stdin/stdout andblocks for each answer; it never sees a token, the database, or any server
state.
Budgets:
script_timeout_ms, node timeout)vm.Interruptwatchdog; parent force-kills after a 2 s gracescript_memory_mb, default 128)debug.SetMemoryLimit+ in-workerHeapAllocmonitor (interrupt)/proc/<pid>/statmand kills past baseline + budgetconcurrent_scripts)handleAPIRequest/strictScriptOutputin the workerA killed or over-budget worker fails the node with a bounded, explainable
error (
execute_script: script worker memory limit of N MiB exceeded) that theaction execution log records; the API process is unaffected.
Acceptance criteria
RSS is untouched; the worker is stopped by its heap monitor and the Linux
RSS backstop. Tested.
executions in the same process. Tested.
unchanged and still tested.
trust level). Option 1 makes it unnecessary:
action.manageremains theauthoring gate, and isolation removes the need to restrict authorship. The
existing
action.manageauthorization and audit are unchanged.credential lifecycle are untouched; the existing bridge tests pass.
Scope note
Condition scripts (
script_engine.go) stay in-process. They are admin-gated(
ConditionSetApplicationService.requireAdmin), capped byscript_timeout_msand the 10 KiB source limit, and have no
windshift.api.requestbridge. Theycan be routed through the same worker in a follow-up; the worker protocol
already separates the bridge from execution. This is called out in the
operator-controls README.
Schema / config
Adds the operator control
script_memory_mb(default 128) tooperatorcontrols.Limits, documented in the operator-controls README andregenerated into
api/openapi-v2.json.Testing
Companion tests: Windshiftapp/core-tests-internal#73.
All passed (services 172 s).
golangci-lint(full repo),make openapi-v2-check, and the pre-commit guards pass.Performance
One
fork/execof the (already-running) server binary per script node. Theitem allows a per-execution worker; a pooled/recycled worker is a drop-in
replacement behind
ScriptWorkerLauncherif spawn cost ever matters.