Skip to content

Expose sqlite3_interrupt() as db.interrupt() — closes #406#407

Open
benhoweblog-dot wants to merge 1 commit into
OP-Engineering:mainfrom
benhoweblog-dot:feat-expose-sqlite3-interrupt
Open

Expose sqlite3_interrupt() as db.interrupt() — closes #406#407
benhoweblog-dot wants to merge 1 commit into
OP-Engineering:mainfrom
benhoweblog-dot:feat-expose-sqlite3-interrupt

Conversation

@benhoweblog-dot
Copy link
Copy Markdown

Closes #406.

Summary

Exposes SQLite's native sqlite3_interrupt() as a db.interrupt() method on the database handle. It aborts any pending operation on the connection, is safe to call from a different thread per SQLite's contract, and rolls back in-flight transactions on interrupt.

Changes

  • cpp/DBHostObject.cpp - JSI binding that calls sqlite3_interrupt(db) on the underlying connection
  • src/functions.ts / src/types.ts - TypeScript wrapper and DB interface exposing interrupt(): void
  • src/functions.web.ts - explicit unsupported web stub for type/API parity
  • docs/docs/api.md and README.md - documentation for the new API
  • example/src/tests/queries.ts - tests for no-op interrupt and aborting a long recursive CTE with rollback

Why

Without this, JS-side timeouts on long queries can only mark a query "cancelled" at the orchestration layer; the underlying SQLite work continues to consume resources until it naturally finishes or the app is killed. This aligns op-sqlite with other SQLite bindings that expose this primitive.

Filed as a need by Pangea, a React Native health/nutrition app. The same need has also been raised in the JS SQLite ecosystem against sql-js/sql.js as sql-js/sql.js#545.

Testing

  • corepack yarn typecheck
  • corepack yarn test:node
  • git diff --check

I wasn't able to run the React Native native example suite in this environment, but I added example coverage for the native API path.

Happy to iterate on API name, error surface, docs wording, or test placement.

Comment thread cpp/DBHostObject.cpp
#ifdef OP_SQLITE_USE_LIBSQL
throw std::runtime_error("[op-sqlite][interrupt] sqlite3_interrupt is not "
"supported with libsql");
#else
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you are missing turso

Comment thread cpp/DBHostObject.cpp
throw std::runtime_error("[op-sqlite][interrupt] database is null");
}

sqlite3_interrupt(db);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You also probably want to call interrupt on close() and delete()?


const queryPromise = db.execute(longQuery);

await new Promise((resolve) => setTimeout(resolve, 50));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is already a sleep function somewhere on the test suite, please use that

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.

Feature: expose sqlite3_interrupt() to allow cancellation of in-flight queries

2 participants