Principle:2giosangmitom Sqmeow nvim Async Execution
| Knowledge Sources | |
|---|---|
| Domains | Query_Execution, Cancellation |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
An asynchronous execution principle that accepts RPC requests immediately, returns an acknowledgement before work starts, and reports completion through events, with cooperative cancellation and deadline enforcement.
Description
Async Execution is the principle of handling long-running database operations (query execution, edit application) by returning an acknowledgement immediately and spawning the actual work as a separate async task. The reply is queued before the work starts so the editor can record the accepted id before handling its events. Completion (success, error, or cancellation) is reported through event notifications.
This principle addresses the challenge of running database operations that may take seconds or minutes: blocking the RPC channel would freeze the editor. By accepting first and reporting later, the editor remains responsive and can cancel in-flight work.
Key mechanisms:
- Acceptance-before-work: The RPC reply (with the call id) goes out before
tokio::spawnstarts the work. Events always follow the reply. - Cooperative cancellation: A
CancellationTokenis registered with the session;canceltrips it. Adapters must stop or drain driver work safely before the connection can be reused. - Deadline enforcement: A
Deadlinetrips the token aftertimeout_ms. Thepassed()flag distinguishes a timeout from a user cancel, so the error message can say which. - Cancellation safety for writes: Write statements are never cancelled mid-flight; only read-shaped batches are interruptible. A cancelled write reports its actual outcome, not an assumed rollback.
Usage
Use this principle when designing an RPC server for long operations: accept immediately, spawn async work, report through events, and support cooperative cancellation with timeout enforcement.
Theoretical Basis
The principle follows the acceptance-before-work pattern:
- Validate and register: Parse args, validate statements, register the call with a cancellation token, allocate a call id.
- Reply: Return the call id as the RPC reply (queued before spawn).
- Spawn:
tokio::spawnthe async work; it runs independently of the handler. - Execute: Run statements through the adapter, respecting the cancel token and deadline. Each statement's result is retained.
- Report: Emit a
call:stateevent with the outcome (done, error, cancelled, timeout).
# Abstract pattern (NOT real implementation):
async def on_request(method, params, reply):
args = parse(params)
call_id = session.next_call_id()
reply.ok(call_id) # acceptance BEFORE work
tokio.spawn(run(call_id, connection, statements))
# run() emits call:state when done
async def run(call_id, connection, statements):
running = session.begin_call(call_id)
deadline = Deadline.start(running.token(), timeout_ms)
result = await backend.execute(sql, max_rows, running.token())
session.store_run([Call(call_id, result)])
emit_call(call_id, "done", summarize(result))
Related Pages
Implemented By
- Implementation:2giosangmitom_Sqmeow_nvim_Backend
- Implementation:2giosangmitom_Sqmeow_nvim_Deadline
- Implementation:2giosangmitom_Sqmeow_nvim_Session