Repository navigation
Stop JavaScript when the connection is interrupted - #11
Conversation
JavaScript runs inside the SQL statement that calls it, but QuickJS had
no interrupt handler, so sqlite3_interrupt() could not stop code that
never returns to SQLite. `SELECT js_eval('while (true) {}')` hung the
sqlite3 shell, where Ctrl-C calls sqlite3_interrupt(), and an app could
not cancel the query.
Install a QuickJS interrupt handler that reports the connection's
sqlite3_is_interrupted(). QuickJS then stops the code with an
uncatchable "interrupted" error, and the statement fails as interrupted.
sqlite3_is_interrupted() is new in SQLite 3.41.0, so the handler is only
installed when the library is at least that version.
Add an Interrupt test that interrupts endless loops in js_eval() and in a
scalar function from another thread, and checks that the connection
still works afterwards.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
…t-memory The next commit keeps garbage collection working near the memory limit from the interrupt handler that sqliteai#11 adds, so this PR now includes sqliteai#11. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
|
Merged into Verified locally on macOS/arm64, which complements your x86_64 Linux testing: universal (x86_64 + arm64) build with no new warnings, the full I confirmed the new Interrupt test is load-bearing: with the I also checked the API assumptions: One small note, not a defect: the handler is installed before Nothing needed changing in this PR. Thanks — this is a clean, well-scoped fix with a test that actually fails without it. |
Two unrelated breakages, on the two platforms the merged PRs were never built on. CI had not run on the pull requests themselves, so neither showed up before the merge. musl (both arches): the extension failed to load at all, with SQLite reporting "error during initialization:" and no message, which is what it says when the entry point returns non-OK without setting pzErrMsg. PR #17's js_stack_room() asks pthread_getattr_np() for the thread's stack. For the initial thread musl reports only the part of the stack that is mapped so far -- 132 KB at load time, measured on musl 1.2.6 -- rather than the 8 MB it may still grow to; glibc reads /proc/self/maps and reports all of it. Taking the 256 KB margin off 132 KB left zero room, globaljs_init() then set a stack size of 1 byte, and JS_NewContext() could not create a context at all, so globaljs_init() returned NULL. RLIMIT_STACK is how far the initial thread may grow, so it is the bound that counts there; the thread is recognized by gettid() == getpid(). Both libcs now measure the same stack, 8,123,840 on musl against 8,123,808 on glibc. The fallback was the worse half of the bug: a measurement that leaves no room must not cripple the runtime. QuickJS reads a size of 0 as "no check at all" and cannot create a context below about 16 KB (measured: 4 KB fails, 16 KB works), so a stack smaller than twice the margin is now split rather than reported as no room, and anything under SQLITE_JS_STACK_MIN is treated as no measurement, leaving the default alone. Missing a runaway recursion is the lesser evil next to an extension that cannot load. A thread with a 64 KB stack now loads the extension, runs JavaScript, and still stops endless recursion with an error. Windows: PR #11's test called alarm(30) to avoid hanging forever if JavaScript cannot be interrupted. alarm() is POSIX only and mingw does not declare it, so test/main.c did not compile. Replaced with a watchdog thread, using the usleep() and pthreads the test already relies on there; verified that it still fires, and aborts with a clear message, when the interrupt handler is removed. Verified green on macOS/arm64, Alpine musl, Debian glibc and under ASan + UBSan: 15/15 sections, no leaks, no sanitizer reports, and no new warnings from sqlitejs.c on either arch. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Follow-up: this broke the Windows build after merge, fixed in c8ae85a.
Replaced with a watchdog thread, using the CI is green on all 15 jobs now. Nothing wrong with the change itself. |
|
Thank you for checking the API guard and for confirming the test fails without the change. Agreed on the ordering: assigning This reply originated with AI: Claude, Anthropic's AI model, wrote it for App Dev For All. Contact: [email protected] |
Nine PRs landed since 1.3.7 (#11 through #19) and none of it was released or recorded in the changelog. Bump SQLITE_JS_VERSION and write the entry. Every claim was checked against a build of 1.3.7 rather than taken from the PR descriptions, which corrected four of them: - js_set_max_stack_size() already existed in 1.3.7, so what changed is that it now rejects a size larger than the calling thread's stack, not the function. - Whole numbers were not simply REAL before. 1.3.7 branched on the value's tag in the engine, JS_TAG_INT, not on the value, so a function returning its REAL argument gave back 3.0 while one computing 6 / 2 gave the integer 3. The entry describes that inconsistency instead of a blanket "REAL became INTEGER". - The BLOB work covers more than results: a typed array passed to db.exec() used to bind NULL, and a function could not return the BLOB it was handed, since a BLOB argument arrives as an ArrayBuffer and returning it produced NULL. - A buffer whose bytes cannot be read, detached or resized out of its view, used to bind NULL and report success. That is a 1.3.7 bug, so it is under Fixed. The memory limit is listed as a breaking change, not only as a feature. 1.3.7 had no limit, so SQL that allocates more than the new 64 MB default now fails with "out of memory"; it is the change most likely to stop working queries, and the entry gives both ways back (js_set_memory_limit(0) and the build flag), each verified. Package.swift is left alone, since the release job rewrites its URL and checksum once the assets exist. Co-Authored-By: Claude Opus 5 <[email protected]>
sqlite3_interrupt()cannot stop JavaScript that never returns to SQLite: QuickJS has no interrupt handler, soSELECT js_eval('while (true) {}')hangs thesqlite3shell (whose Ctrl-C callssqlite3_interrupt()) until the process is killed, and an app cannot cancel the query.This installs a QuickJS interrupt handler that reports
sqlite3_is_interrupted()for the connection. QuickJS then stops the code with an uncatchable "interrupted" error, even insidetry/catch, and the statement fails as interrupted.sqlite3_is_interrupted()is new in SQLite 3.41.0, so the handler is installed only whensqlite3_libversion_number()is at least that. The README says so under JavaScript Evaluation.Test: a new Interrupt test interrupts, from another thread, an endless loop in
js_eval(), one in a scalar function, and one that catches every exception, and checks that the connection works afterwards. Without the change the test hangs until itsalarm()ends the program.make testpasses on x86_64 Linux, also under ASan and UBSan.These are six separate pull requests, #11 to #16, so each can be taken or declined on its own; only #15 depends on another (#14). They add tests at the same places in
test/main.c, and #11 and #12 both add code just aboveglobaljs_init(), so those merged later will need a small rebase, which we will do.This pull request originated with AI. Claude, Anthropic's AI model, wrote the code, the tests and this description while packaging sqlite-js for Code on the Go, App Dev For All's Android IDE, which ships sqlite-js with this change. Contact: [email protected]
🤖 Generated with Claude Code
https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw