Repository navigation
feat(napi): expose sync StatementCanceller via executeStatementCancellable - #122
Conversation
…lable The blocking `Connection.executeStatement` builds a kernel `Statement`, awaits `execute()` (which polls to terminal), and only then hands JS a `Statement` wrapper — so a query running for several seconds server-side is uncancellable from JS on that path (no handle until the blocking call resolves). Add `Connection.executeStatementCancellable(sql, opts)` returning a `CancellableExecution` handle that captures a detached `StatementCanceller` BEFORE dispatching `execute()`, then exposes: - `result()` — drives the blocking execute, resolves to a `Statement` identical to what `executeStatement` returns; - `cancel()` — fires the detached canceller lock-free, interrupting a still-running `result()` mid-COMPUTE; - `statementId` getter for log correlation while in flight. Mirrors the pyo3 `Statement.canceller()` / `Statement.execute()` split (PR #121) and the napi `AsyncStatement` detached-canceller pattern. Co-authored-by: Isaac Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
Adds the missing live e2e coverage for the sync-cancel surface (the `StatementCanceller` from #121 that #122 exposes over napi): obtain a detached canceller before execute(), drive a ~1e12-row cross join, cancel 13s in (past the ~10s wait-timeout window so the statement_id is published), and assert the blocking execute() rejects instead of running to completion. Finding documented in the test: the sync execute() path currently surfaces a server-side cancel as `InvalidArgument` (the async path returns `Cancelled`); the napi/pyo3 wrappers normalise it to a cancelled status client-side. The assertion accepts both so it stays green before and after that is normalised. Verified live (13.6s; statement cancelled server-side). Co-authored-by: Isaac Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
|
P1 — Should fix
P2 — Minor
|
…l RPC Addresses the #122 review (gopalldb): - P1.2: `CancellableExecution::result()` now normalises the kernel's sync-cancel quirk — `execute()` surfaces a server-side cancel as `InvalidArgument` (the async path returns `Cancelled`) — into `Cancelled` when this handle's `cancel()` fired (tracked via a `cancel_fired` AtomicBool). So every napi consumer sees a single cancelled-status code regardless of execution path, rather than relying on each wrapper's own client-side mapping. Verified end-to-end against a live warehouse: a mid-compute `cancel()` makes `result()` reject with `{"code":"Cancelled"}` (was `InvalidArgument`). - P2.3: `cancel()` skips the server-side cancel RPC once `result()` has finished successfully (a `finished` AtomicBool), matching the post-completion gating on `AsyncStatement`/`Statement` — no redundant no-op-at-server call / confusing log line. - P2.4: documented the API-shape asymmetry vs pyo3 (napi exposes a single `Connection.executeStatementCancellable -> CancellableExecution`; pyo3 splits it across `Statement.canceller()` / `Statement.execute()`). - P1.2 doc: corrected the e2e test comment — the core `Statement::execute()` still returns `InvalidArgument`; the napi layer is what normalises to `Cancelled` (this PR). pyo3 does not yet. cargo fmt / clippy -D warnings / `cargo test --lib` (620) green; core e2e sync-cancel green. Co-authored-by: Isaac Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
|
Thanks @gopalldb — addressed in P1.2 (the important one) — fixed, not just documented. You were right that no mapping existed and P2.3 — P2.4 — added the pyo3 API-shape note to the module rustdoc (napi: one P1.1 (test coverage) — option (b) + verification, with a note on why (c) isn't feasible. A pure offline Rust unit test of
I also left a note that the core-level This reply was written by Isaac. |
|
P1 — Should fix
JS callers writing if (err.code === 'Cancelled') {...} else { reportSqlError(err) } see "cancelled" for a query that actually failed due to a SQL bug. Observability broken. Fix: only set cancel_fired = true after the canceller actually dispatched a wire cancel. Gate on canceller.statement_id().is_some() before storing: P2 — Minor
|
…testable Addresses the #122 round-2 review (gopalldb): - P1.1 (correctness bug): a first-window `cancel()` (before the statement_id is published) dispatches no RPC, yet it was still setting `cancel_fired` — so a later GENUINE `InvalidArgument` (a real SQL/parse/type error) got mislabelled `Cancelled` and its diagnostic discarded. `cancel()` now sets `cancel_fired` only when a server-side cancel actually dispatched (statement_id observed), so the normalisation keys on "we issued a cancel", not "JS called cancel()". - P1.2 (testability): factored the error-normalisation into a pure `normalize_execute_err(err, cancel_fired) -> KernelError` and added offline unit tests — (InvalidArgument, dispatched)→Cancelled, (InvalidArgument, not-dispatched)→InvalidArgument (diagnostic preserved), (NetworkError, dispatched)→NetworkError. (napi crate: 44 tests.) - P2.3: `terminal` flag (renamed from `finished`) is now set in BOTH arms of `result()` (success and error), so a defensive `cancel()` after any terminal state skips the redundant RPC — not just after success. - P2.4: the flag-ordering race is resolved as a side effect of P1.1 (the flag is only set after dispatch). - P2.5: documented the three racing outcomes (natural completion / dispatched cancel / dropped-future drop-guard) and which one yields a Cancelled code. fmt / clippy -D warnings / napi `cargo test` (44) green; happy-path mid-compute cancel re-verified live → `result()` rejects with `{"code":"Cancelled"}`. Co-authored-by: Isaac Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
|
Great catch on P1.1 — that was a real bug I introduced with the normalisation. Fixed in
fmt / clippy -D warnings / napi This reply was written by Isaac. |
… the normalisation race
The round-2 fix gated the InvalidArgument→Cancelled normalisation on a
pre-check `canceller.statement_id().is_some()` before calling `cancel()`.
That pre-check is a TOCTOU: the server statement_id can be published (by the
concurrent `execute()`'s POST) in the window between the pre-check reading
`None` and the canceller's own lock-read seeing the id and dispatching a real
cancel. In that window a genuinely-cancelled execute would surface its
`InvalidArgument` un-normalised (shown as a SQL error instead of Cancelled).
It failed safe, but it was not race-free.
Fix: `StatementCanceller::cancel()` now returns `Result<bool>` — `true` iff it
actually dispatched a cancel RPC — decided by the SAME id read that gates the
dispatch. The napi `CancellableExecution::cancel()` sets `cancel_fired` from
that return value instead of a separate racy pre-check, so the flag exactly
reflects "a cancel was issued" with no time-of-check/time-of-use gap.
`AsyncStatementCanceller` (the async path) is a distinct type and is unchanged.
core: fmt / clippy -D warnings / `cargo test` (23 lib) green.
napi: fmt / clippy -D warnings / `cargo test` (44) green; the three
`normalize_execute_err` unit tests unchanged. Mid-compute cancel re-verified
live via the node binding → `result()` rejects with `{"code":"Cancelled"}`.
Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
|
Following up on my own P1.1 fix — I wasn't satisfied it was fully race-free, and on a second look it wasn't. Pushed The residual race in the Fix. Scope note: this does now touch core ( core: fmt / clippy -D warnings / This reply was written by Isaac. |
|
One more verification, since this round touches a core signature ( pyo3 is unaffected — verified, not assumed. Python's Known residual (will file a follow-up). The normalisation keys on a code-level heuristic: the kernel's sync This reply was written by Isaac. |
What
Expose the sync
StatementCancellerover the napi binding (mirroring pyo3'sStatement.canceller()), so a multi-second blockingexecuteStatementis cancellable from JS — the napi hole left by the pyo3 sync-cancel work in #121.Surface (napi only — zero kernel-core changes)
napi/src/cancellable.rs→CancellableExecutionnapi class.Connection.executeStatementCancellable(sql, opts) -> CancellableExecution: builds the kernelStatement, captures a detachedStatementCancellerbefore dispatchingexecute(), then exposesresult()(drives the blocking execute →Statement),cancel()(lock-free; interrupts a runningresult()mid-compute), andstatementId(string | null).StatementCanceller,inflight_id,POST /statements/{id}/cancel) is unchanged kernel core from feat(statement): tz-aware/scientific param binds, error context, sync cancel + Ctrl-C #121.Cancel-status normalisation (review P1.2)
On a server-side cancel the core
Statement::execute()surfacesInvalidArgument(a kernel quirk — the async path returnsCancelled).CancellableExecution::result()now normalises that toCancelledwhen this handle'scancel()fired, so every napi consumer sees one cancelled-status code regardless of path. Verified end-to-end against a live warehouse driving the raw binding: a mid-computecancel()makesresult()reject with{"code":"Cancelled","message":"statement cancelled by client"}(previouslyInvalidArgument).Other review items
cancel()skips a redundant server-side RPC onceresult()finished successfully (afinishedgate, matchingAsyncStatement/Statement).Tests
cargo fmt/clippy -D warnings/cargo test --lib(620) green.tests/v0_execute_e2e.rs::sync_canceller_cancels_a_running_execute_mid_compute) exercising theStatementCancellermechanism end-to-end (verified live, 13.6s; statement cancelled server-side).CancellableExecutionsurface (thestring|nullgetter, double-result()closed-err, cancel-before-id no-op, and the newCancellednormalisation) is consumed and exercised end-to-end by databricks-sql-nodejs#416 — node unit tests for the cancellable path plus a live mid-compute cancel verified server-side, and the raw-bindingCancelled-normalisation check above. A self-contained kernel-side napi test isn't possible as a pure unit test (the napi modules arepub(crate)andCancellableExecutionrequires a session); a wiremock-backed napi integration test is a reasonable follow-up. Recommend gating final merge on chore: bump version to 1.3.0 #416's CI.Remaining kernel-core follow-up (not in this PR)
The sync
execute()path returningInvalidArgumentrather thanCancelledis a core quirk; this PR papers over it at the napi layer. Normalising it in the core (so pyo3 and direct Rust callers benefit too) is the proper follow-up.This pull request and its description were written by Isaac.