Skip to content

Let a host cancel running JavaScript when it cannot call sqlite3_interrupt() (Android) #20

Closed

Description

@davidschachterADFA

Following up on your note in #19: on Android, sqlite3_interrupt() does not reach JavaScript that never returns, because an app has no way to call it on the right connection.

Why. The SQLite wrappers an Android app uses, the framework's android.database.sqlite and requery's sqlite-android (which Room uses here), cancel a query by setting a flag that their progress handler returns (nativeCancel in android_database_SQLiteConnection.cpp). They never call sqlite3_interrupt(), and they do not give the app the native sqlite3 * of the connection a query runs on. SQLite does not call the progress handler while a function runs, so a scalar function, aggregate or collation that loops never stops, and it holds a connection and a core until the process ends.

What we carry. One patch on top of main (c8ae85a), in the branch host-cancel-all of davidschachterADFA/sqlite-js (d703446). It adds void sqlitejs_cancel_all(void), which stops all JavaScript that is running at that moment, in every connection:

  • A global epoch is bumped by sqlitejs_cancel_all() and read when the outermost call into JavaScript begins (js_enter). The interrupt handler that Stop JavaScript when the connection is interrupted #11 added compares the two, so the cancel reaches every runtime without a list of connections. Safe from any thread.
  • When the cancel stops a call, the handler also calls sqlite3_interrupt() on that call's connection. A collation cannot report an error: without this, a sort goes on calling it, each call beginning after the cancel, and never ends.
  • The interrupt handler is installed even when sqlite3_is_interrupted() is unavailable (SQLite before 3.41), so a cancel still works there.

An app calls it when a coroutine or task is cancelled, and keeps calling it until the query ends, since JavaScript that begins after a cancel runs normally.

Limits. It stops all JavaScript running in the process at that moment, not one query's. SQL that JavaScript runs itself through db.exec finishes before the cancel takes effect, when JavaScript next runs.

Tests. A Cancel all test in test/main.c stops a scalar function that catches every exception and a runaway collation, then checks that JavaScript runs normally afterwards, on that connection and on a new one. It passes with the full suite, 16 of 16, on x86_64 Linux and under ASan + UBSan. With the sqlite3_interrupt() call removed, the collation case runs until the test's watchdog stops it. We use it in an Android library built on Room, where a cancelled query stops in about 5 ms.

If you would like this upstream, we can open a pull request from that branch, or adapt it to an API you prefer, such as a per-connection handle.

This issue originated with AI: Claude, Anthropic's AI model, wrote it and the patch for App Dev For All, which ships sqlite-js in Code on the Go. Contact: [email protected]

Activity

  1. davidschachterADFA commented on Nov 12, 2026

    @davidschachterADFA
    ContributorAuthor

    A fix to the patch this issue offers: the branch host-cancel-all now has a second commit (6abfbe2).

    A sqlitejs_cancel_all() that landed while another thread opened a connection made that connection fail to load the extension, with SQLITE_NOMEM. Setting up the runtime runs JavaScript of its own (the typed-array getters), and a new context polls the interrupt handler on its first call, so the cancel stopped it. In an app, a query that opened a new pool connection then failed. Now only JavaScript inside a call (js_enter, depth > 0) is cancelled. Cancel all now also opens 200 connections while another thread cancels without pause: the first one failed before, and the suite passes 16 of 16 with the change.

    The same branch also rebases cleanly onto 1.4.1. #22 and #23 are separate fixes, which we found while moving to 1.4.1.

    This comment originated with AI: Claude, Anthropic's AI model, wrote it and the fix for App Dev For All. Contact: [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions