Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2giosangmitom Sqmeow nvim Async Execution

From Leeroopedia
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::spawn starts the work. Events always follow the reply.
  • Cooperative cancellation: A CancellationToken is registered with the session; cancel trips it. Adapters must stop or drain driver work safely before the connection can be reused.
  • Deadline enforcement: A Deadline trips the token after timeout_ms. The passed() 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:

  1. Validate and register: Parse args, validate statements, register the call with a cancellation token, allocate a call id.
  2. Reply: Return the call id as the RPC reply (queued before spawn).
  3. Spawn: tokio::spawn the async work; it runs independently of the handler.
  4. Execute: Run statements through the adapter, respecting the cancel token and deadline. Each statement's result is retained.
  5. Report: Emit a call:state event 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

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment