Skip to content

feat: expose cancellation for async SQLite reads - #544

Draft
chrispader wants to merge 6 commits into
fix/queued-read-cancellationfrom
feat/abortable-sqlite-reads
Draft

chrispader wants to merge 6 commits into
fix/queued-read-cancellationfrom
feat/abortable-sqlite-reads

Conversation

@chrispader

@chrispader chrispader commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

A read that an app no longer needs can hold up the next query on the same connection. Pass an AbortSignal to a regular or prepared async read to remove it from the queue or stop it while SQLite is running. The promise rejects with OperationCancelled if cancellation wins, and the connection remains available for later queries.

const controller = new AbortController()
const result = db.executeAsync('SELECT * FROM notes', undefined, {
  signal: controller.signal,
})
controller.abort()

The Node mock supports cancellation before execution; preparation-time authorization still needs native tests. See the mock limits.

Builds on #536. Fixes #400.

Note

🤖 Generated with Codex

@vercel

vercel Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
react-native-nitro-sqlite Ready Ready Preview Dec 4, 2026 12:29pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@chrispader chrispader left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Chris's AI understudy stepping in (Claude Opus 5.5). 🤖

The core cancellation logic is solid. The gaps are in tests, docs, and parity between the mock and native. I reviewed this PR and Callstack's Apex model reviewed it separately. Neither of us found a race: an already-aborted signal rejects before any native work, listeners are removed on every exit path, row batches that arrive after a rejection are dropped, and each operation settles once. Drizzle and TypeORM stay source-compatible, because the new options argument is optional and the transaction methods leave it out.

  • The new device spec has never run in CI. The Android, iOS and macOS jobs were skipped, so readCancellation.spec.ts has only run under the C++ and Jest harnesses, where native is mocked. Please run it on at least one device before merging. (Claude)
  • Two cases from #400 have no test. One closes the connection while an interrupt is in flight. The other starts a second cancellable read right after a cancel. Every follow-up in the spec today is non-cancellable (prepared.executeAsync([3])), so the paused SELECT 1 anchor and the leftover interrupt flag are never exercised. Each is about ten lines in the existing spec. (Apex)
  • The mock and native disagree on what counts as a read. The mock checks the leading keyword, so it rejects (SELECT 1), which native accepts, and accepts EXPLAIN INSERT …, which native denies. A query can pass in Node tests and fail on a device, or the reverse. (Apex, inline)
  • The error docs now contradict the code. parameters-and-results.mdx still lists the native categories up to NoBatchCommandsProvided. It also says error.type is undefined for queue errors, but a cancelled queued read now carries type: 'OperationCancelled'. (Apex)
  • Smaller items:
    • Aborts drop signal.reason, so AbortSignal.timeout() callers can't tell a timeout from a manual abort. (Claude, inline)
    • getReadCancellation uses an unchecked static_pointer_cast. (Apex, inline)
    • docs/scripts/check-api.mjs doesn't guard createReadCancellation or ExecuteAsyncOptions. (Apex)

Apex also flagged the authorizer cost from #536. I'll put that on #536, since that's where the code lives.

// better-sqlite3 does not expose SQLite's authorizer. Check the first keyword
// before preparation because some PRAGMAs take effect during prepare().
const readPrefix =
/^(?:\s|;|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)*(?:SELECT|VALUES|WITH|EXPLAIN)\b/i

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex: Native decides with the authorizer and sqlite3_stmt_readonly, but this regex decides on the first keyword. (SELECT 1) is rejected here and allowed natively. EXPLAIN INSERT … is allowed here and denied natively. Read-only PRAGMAs are rejected by both, but with a different error.type. Since better-sqlite3 exposes statement.readonly, preparing first and checking that would match native more closely, except for the PRAGMA-at-prepare case this comment mentions.

import NitroSQLiteError, { NitroSQLiteExceptionType } from './NitroSQLiteError'

export function readCancellationError(): NitroSQLiteError {
return new NitroSQLiteError('SQLite read was cancelled', {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude (low): Each abort rejects with a fresh error and drops signal.reason. Callers using AbortSignal.timeout(500) can't tell a timeout from a user abort. NitroSQLiteError already accepts ErrorOptions, so passing { cause: signal.reason } through would fix it.

if (!cancellation) {
return nullptr;
}
return std::static_pointer_cast<HybridNitroSQLiteReadCancellation>(*cancellation)->state;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex (low): NitroSQLiteReadCancellation is reachable from JS, so this argument arrives typed as the spec. If anything other than this C++ class ever implements it, the static_pointer_cast is undefined behavior instead of an error. dynamic_pointer_cast with a clear exception is the same size.

chrispader and others added 6 commits December 4, 2026 13:27
Accept an AbortSignal for managed and prepared reads, preserve native FIFO ordering, and expose an OperationCancelled error when cancellation wins.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Keep AbortSignal reasons across queue and native cancellation. Reject unsafe EXPLAIN preparation in the mock, check native token implementations, and document transaction and error contracts with regressions.

Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>

This branch was successfully deployed

1 active deployment
Preview — a98c5463 Deployed Dec 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: cancel queued and active SQLite reads

1 participant