Repository navigation
Let a host cancel all running JavaScript, and release 1.4.3 - #34
Conversation
sqlitejs_cancel_all() stops the JavaScript running in every connection: each call fails with an "interrupted" error, even code that catches every exception. JavaScript that begins afterwards runs normally. Android cannot otherwise stop a runaway function: it cancels a query with a progress handler, which SQLite never calls while a function runs, and Room 2.7 and later pass no cancellation signal at all. A global epoch, read when the outermost call into JavaScript begins and compared by the existing interrupt handler, makes the cancel reach every runtime without a list of connections. The interrupt handler is now installed even when sqlite3_is_interrupted() is unavailable (SQLite before 3.41), so cancelling still works there. When the cancel stops a call, the handler also interrupts that call's connection with sqlite3_interrupt(), so its statement stops too. A collation cannot report an error: without this, a sort went on calling it, each call beginning after the cancel, and never ended. Add to the Cancel all test a runaway collation that one cancel stops; it hung without this. The test's worker cancels only once the JavaScript under test has called started(), an SQL function, since a cancel that came first would leave the JavaScript running. If that has not happened within 10 seconds, the worker fails the test with a message instead of cancelling. Like the Interrupt test, a watchdog ends the program if the JavaScript cannot be stopped. Only a call into JavaScript is cancelled, not the JavaScript a connection runs while it is set up: a cancel then made the connection fail to load the extension (SQLITE_NOMEM), and an app's query on a new pool connection failed. The Cancel all test now opens 200 connections while another thread cancels without pause; without this, the first failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Since 1.4.2 every result set still open when the outermost call into JavaScript returns is ended there, which runs the final of each window or aggregate function its statement was computing. Each final enters JavaScript as a new outermost call, so js_enter() took the cancel epoch of that moment and a cancel during the call no longer stopped it. On SQLite 3.41 and later the interrupt the cancel left on the connection does; before 3.41 nothing did, and a final that never ends hung the statement for good. The finals that ending a call's result sets runs now keep that call's epoch, so a cancel during the call, or during the ending, stops each of them at QuickJS's next interrupt check: one that never ends is stopped and the statement fails with an interrupt, though a short final can still run to its end. Reading on from those result sets throws "interrupted", as after sqlite3_interrupt() on 3.41 and later, rather than the message for a result set that outlived its call, whether the cancel came before the call returned or while its result sets were ended. js_end_rowsets() marks the latter after its loop, by the pass that ended them, since a final it runs can free result sets of the list. Before 3.41, an sqlite3_interrupt() that no cancel came with still reads as the result set outliving its call: nothing shows it. Test: Cancel all now covers both cases on any SQLite. A call that returns normally keeps two result sets of window functions whose finals never end, and the first final starts the cancel: both finals stop and both result sets read on as "interrupted". And a cancel that lands as a call returns, from SQL inside it, stops the long final that ending its result set runs, so the call fails and the final records nothing. Without this change the first fails with the "closed" message, and the second returns OK with the final run to its end. Upstream's suite passes 17 of 17, also under ASan, LeakSanitizer and UBSan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
The DLL exports only what the generated .def file lists, so the test
program could not link sqlitejs_cancel_all() on Windows ("undefined
reference" in the windows-x86_64 job). The .def file now lists it with
the other public functions; it is the only export list in the project.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
- js_leave no longer tests the cancel epoch: the block after the loop in js_end_rowsets already marks a cancelled call's result sets interrupted. - Say that only JavaScript the extension runs while it initialises a connection (globaljs_init, depth 0) is exempt from a cancel; stored functions loaded by js_init_table run as a call and can be cancelled. - Initialise rowset end_pass to 0 where a rowset is created. - Document sqlitejs_cancel_all in sqlitejs.h. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
sqlitejs_cancel_all() exempted only the JavaScript run while the extension loads (globaljs_init, depth 0). The functions js_init_table(1) restores from the js_functions table ran as an ordinary call, so a cancel in flight stopped one: js_init_table(1) failed with "interrupted" and the connection came up without the function, reported only by a statement an app need not look at. With an init long enough to reach one of QuickJS's interrupt checks every load failed, which is exactly the Android pool this feature is for: a connection opened during a cancel could not run a query of its own. Loading a stored function is setting the connection up, as loading the extension is, so it is exempt too. js_create_common() records the depth of an is_load call in setup_depth and the interrupt handler leaves that depth alone. Only that call is exempt: JavaScript the loading code reaches through SQL is a call of its own, at a greater depth, and is still cancelled, so a runaway init can be stopped. On SQLite 3.41 and later sqlite3_interrupt() still stops setup JavaScript that never ends, as it always has. sqlitejs.h now also states what a cancel does besides stopping JavaScript: the connection is interrupted as sqlite3_interrupt() does it, which a collation makes necessary, so any other statement that connection has in progress fails with SQLITE_INTERRUPT and sqlite3_is_interrupted() stays true for it until the connection next prepares or steps a statement with none of its own running. Test: Cancel all now restores a stored aggregate, whose init code is long enough to reach an interrupt check, on ten connections while another thread cancels without pause, and checks the function it loads computes what it should once the cancels stop. Without the exemption the first connection fails. The suite passes 17 of 17, also under ASan, LeakSanitizer and UBSan, and ThreadSanitizer reports only the test watchdog's own pre-existing race. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
SQLITE_JS_VERSION 1.4.2 -> 1.4.3, which is what the release is tagged with and what sqlitejs_version() reports: the release job reads make version, finds it differs from the latest release, and tags, publishes and releases under it. The node package.json and the Flutter pubspec.yaml are left alone, since that job writes them in its own [auto-update] commit. The changelog gains a 1.4.3 section for sqlitejs_cancel_all(): the function itself and what it does to the connection it stops, the result sets a cancelled call leaves open, and the setup JavaScript a cancel never stops. A patch release by the only measure that matters to existing code: nothing an application already does behaves differently, since all of this starts at a call to a function that did not exist before. It does add a C entry point, which Keep a Changelog would put in a 1.5.0. The built library reports 1.4.3 and the suite passes 17 of 17 on macOS arm64. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Merged into That push to The four commits of #33 went in unmodified, so GitHub saw them reachable from |
Takes the four commits of #33 as they are and adds two of my own, so merging this
lands that pull request as well. Nothing in it changes for an application that never calls the new
function.
What #33 brings (
c52018c…f0f6216, unmodified):sqlitejs_cancel_all(), whichstops the JavaScript running in every connection, for hosts whose own cancelling cannot reach it —
Android cancels a query with a progress handler, which SQLite never calls while a function runs, and
Room 2.7 and later pass no cancellation signal at all. A global epoch read when the outermost call
begins, compared by the existing interrupt handler, reaches every runtime without a list of
connections; the handler also interrupts the stopped call's connection, which a collation makes
necessary since it cannot report an error; the result sets a cancelled call leaves open read on as
"interrupted" and the finals that ending them runs keep the call's epoch, so one that never ends is
stopped rather than hanging the statement on SQLite before 3.41; and the Windows
.deffile exportsthe new function.
What I verified before changing anything
Built
main(5f0b446) and the pull request side by side on macOS arm64. The suite goes from 16 of16 to 17 of 17 and stays there over 15 consecutive runs and 6 more under CPU load; ASan and UBSan are
clean on both; ThreadSanitizer reports only the test watchdog's own pre-existing race on
test_watchdog_stop, twice instead of once because the new test reuses that watchdog;-Wall -Wextra -Wshadow -Wmissing-prototypesadds no warning on either architecture; the suite takes 0.1 slonger. Simulating SQLite before 3.41, by raising the two
3041000comparisons, the Cancel all teststill passes and nothing hangs, which is the claim the design rests on. The 8 suites I ran in
parallel that failed on
Serialization 1/2/3fail identically onmain: they sharejs_functions.sqlitein one directory.I also wrote eight probes of my own against the pull request. Seven passed: a cancel of a nested
call (JavaScript → SQL → JavaScript), no harm to a second idle connection, a cancel with nothing
running, 40 open/close cycles with window-function result sets open under a storm of cancels (clean
under ASan and TSan), and a cancel during an aggregate's
step. Two found the problems below.b8898db— never cancel the JavaScript that sets a connection upThe exemption covered only the code run while the extension loads (
globaljs_init, depth 0). Thefunctions
js_init_table(1)restores from thejs_functionstable ran as an ordinary call, so acancel in flight stopped one:
js_init_table(1)failed with "interrupted" and the connection cameup without the function, reported only by a statement an app need not look at. Measured with a
stored aggregate whose init code is long enough to reach one of QuickJS's interrupt checks, every
one of 20 loads failed — which is exactly the Android connection pool this feature exists for.
With a short init all 40 succeeded, so it is a matter of timing, not of luck in one direction.
Loading a stored function is setting the connection up, as loading the extension is, so it is exempt
too:
js_create_common()records the depth of anis_loadcall insetup_depthand the interrupthandler leaves that depth alone. Only that call is exempt — JavaScript the loading code reaches
through SQL is a call of its own, at a greater depth, and is still cancelled, so a runaway init can
be stopped — and on SQLite 3.41 and later
sqlite3_interrupt()still stops setup JavaScript thatnever ends. A ninth probe confirms that scoping: a
db.exec("SELECT spin()")run from a loadingfunction's init code is still interrupted.
The same commit documents in
sqlitejs.hwhat a cancel does besides stopping JavaScript, which theheader promised nothing about: the connection is interrupted as
sqlite3_interrupt()does it, so anyother statement it has in progress fails with SQLITE_INTERRUPT — measured, a host cursor mid-
stepgets 9 — and
sqlite3_is_interrupted()stays true for it until the connection next prepares or stepsa statement with none of its own running.
Test: Cancel all now restores that stored aggregate on ten connections while another thread
cancels without pause, and checks the function it loads computes what it should once the cancels
stop. Built against the pull request's own
sqlitejs.cthe new case fails on the first connection,so it holds the fix in place. 17 of 17 again, over 10 runs and 4 under load, clean under ASan and
UBSan, TSan unchanged. The test takes 0.42 s, 1.32 s under ASan and 8.55 s under TSan, against the
30 s watchdog it already had, so I left that alone.
aa88a9e— release 1.4.3SQLITE_JS_VERSION1.4.2 → 1.4.3 and a changelog section for the new function, following62ac846: those two files only, since the release job writes the nodepackage.jsonand the Flutterpubspec.yamlin its own[auto-update]commit. The rebuilt library reports 1.4.3.One thing to settle before merging: Keep a Changelog would put a new C entry point in a 1.5.0, and
the 1.4.2 message said "nothing added … so not 1.5.0". I used 1.4.3 because nothing an application
already does behaves differently, but the call is yours.
For the reviewer
test_watchdog_stopis avolatile intread andwritten across threads, which is the only ThreadSanitizer finding in the suite.
🤖 Generated with Claude Code