fix: Bound the wait for a procedure instance slot and report it - #5875
Open
Ludv1gL wants to merge 3 commits into
Open
fix: Bound the wait for a procedure instance slot and report it#5875Ludv1gL wants to merge 3 commits into
Ludv1gL wants to merge 3 commits into
Conversation
`ModuleInstanceManager::get_instance` acquired a slot in the bounded procedure pool with a bare `Semaphore::acquire_owned().await`. Once every slot was held (a handful per database by default, one per core), every later procedure call, HTTP handler call and scheduled procedure on that database waited forever, and a websocket caller got no result envelope, error or timeout at all. Add `procedure-queue-timeout` to the `[wasm]` and `[v8]` config sections (default 30 seconds, 0 waits forever as before). When it elapses, `get_instance` fails with `InstancePoolTimeout` before anything was started, so the caller can retry. The error reaches: - websocket procedure calls as a `ProcedureStatus::InternalError` result for the request id, instead of silence - HTTP `/call` and module HTTP handlers as 503 Service Unavailable - the scheduler, which re-queues the item instead of dropping it `enqueue_with_procedure_instance` now takes an already acquired lease so the caller can report the timeout while it still owns the result target.
`enqueue_procedure` awaited the pool slot before returning, and the websocket receive task handles one message at a time. With a full pool this stalled every later message from that connection (subscribe, reducer calls, one-off queries) until a slot freed up, or now until the queue timeout elapsed. Only already established subscriptions kept flowing, since the send task is separate. Spawn the checkout and the call as a task once the call is validated, so the receive loop only pays for argument validation. Validation errors are still reported synchronously, and the result or the timeout error is delivered to the same target as before.
Add `spacetime_procedure_instance_pool_timeouts_total`, labelled by database and module type, so a saturated procedure pool shows up in metrics rather than only as a stalled client. The wait itself is already visible for WASM procedures through `spacetime_reducer_wait_time_sec` (the call timer starts before the checkout) and `spacetime_worker_instance_operation_queue_length`.
Ludv1gL
force-pushed
the
fix/procedure-pool-wait-timeout
branch
from
September 5, 2026 00:39
3f9e220 to
19abb54
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
ModuleInstanceManager::get_instanceacquires a slot in the bounded procedure instance pool with a bareSemaphore::acquire_owned().await: no timeout and no bound on waiters. The pool defaults to one slot per core. Once every slot is held (a procedure that does outbound HTTP holds its slot for the whole call), every later procedure call on that database queues forever and the websocket caller never receives a result envelope, an error, or a timeout.It is worse than it looks, because
enqueue_procedureawaited the slot before returning and the websocket receive task (ws_recv_task) handles one message at a time. So a single queued procedure call also stalled every later message from that connection: subscribes, reducer calls and one-off queries. Only subscriptions that were already established kept flowing, since the send task is separate. From the client this looks like "the call never settles, subscriptions are fine".Three commits, each reviewable on its own:
procedure-queue-timeoutin the[wasm]and[v8]config sections (default 30 seconds,0waits forever as before). When it elapses,get_instancefails withInstancePoolTimeoutbefore anything was started, so the call is safe to retry. The error reaches websocket callers as aProcedureStatus::InternalErrorresult for their request id, HTTP/calland module HTTP handlers as 503, and the scheduler re-queues the item instead of dropping it.spacetime_procedure_instance_pool_timeouts_total{db, module_type}. The wait itself is already visible throughspacetime_reducer_wait_time_secandspacetime_worker_instance_operation_queue_length.Not included, on purpose: a max-waiters cap (waiters are now bounded by timeout times arrival rate; a
try_acquirefast-fail can be layered on if that ever matters), and a dedicatedProcedureStatusvariant on the wire (the string status keeps this free of SDK codegen changes).API and ABI breaking changes
None on the wire.
ProcedureCallErrorandHttpHandlerCallErrorgain aPoolTimeoutvariant, andModuleHost::call_procedure_with_paramsnow returnsPooledCallErrorinstead ofNoSuchModule. Both are host-internal types.Expected complexity level and risk
Testing
bounded_pool_times_out_waiting_for_a_slot: a pool of one slot, held; a second checkout fails withInstancePoolTimeout; after the lease is returned, checkout succeeds.options_from_partial_tomlextended to coverprocedure-queue-timeoutas a duration string and as0.cargo clippy --all-targetsclean onspacetimedb-core,spacetimedb-client-api,spacetimedb-standalone;cargo check --workspace --all-targetsclean.Verified locally
Standalone built from this branch,
[wasm] procedure-instance-pool-size = 1,procedure-queue-timeout = "2s", a module withhold(secs)(a procedure thatsleep_untils) andquick().HTTP: with
hold(8)in flight,quick()andhold(1)return503 Timed out after 2s waiting for a free procedure instance; the call was not startedafter 2 s; the holder returns8normally.One websocket connection sending
hold(8),hold(8),quick(),Subscribeback to back:InitialSubscriptionholdandquickInternalErrorresult at +2.07 sholdReturned 8Returned 8