Skip to content

Stop JavaScript when the connection is interrupted - #11

Merged
marcobambini merged 1 commit into
sqliteai:mainfrom
davidschachterADFA:stop-javascript-on-interrupt
Nov 12, 2026
Merged

marcobambini merged 1 commit into
sqliteai:mainfrom
davidschachterADFA:stop-javascript-on-interrupt

Conversation

@davidschachterADFA

Copy link
Copy Markdown
Contributor

sqlite3_interrupt() cannot stop JavaScript that never returns to SQLite: QuickJS has no interrupt handler, so SELECT js_eval('while (true) {}') hangs the sqlite3 shell (whose Ctrl-C calls sqlite3_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 inside try/catch, and the statement fails as interrupted. sqlite3_is_interrupted() is new in SQLite 3.41.0, so the handler is installed only when sqlite3_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 its alarm() ends the program. make test passes 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 above globaljs_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

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
@marcobambini

Copy link
Copy Markdown
Member

Merged into main as part of a review of #11–#19 together.

Verified locally on macOS/arm64, which complements your x86_64 Linux testing: universal (x86_64 + arm64) build with no new warnings, the full test/main.c suite green, and clean under ASan + UBSan with zero reports.

I confirmed the new Interrupt test is load-bearing: with the JS_SetInterruptHandler call reverted and the test kept, the suite hangs until alarm(30) kills the process. The three cases (js_eval, a scalar function, and a loop that catches every exception) all pass with the change.

I also checked the API assumptions: JSInterruptHandler's signature matches libs/quickjs.h, and sqlite3_is_interrupted is entry 363 of sqlite3_api_routines, so the sqlite3_libversion_number() >= 3041000 guard correctly avoids reading past the end of a shorter struct on an older library.

One small note, not a defect: the handler is installed before js->db = db is assigned, so it relies on the memset plus the js->db != NULL check to be safe in that window. It is correct as written, just a little implicit — assigning js->db before installing the handler would remove the window entirely.

Nothing needed changing in this PR. Thanks — this is a clean, well-scoped fix with a test that actually fails without it.

marcobambini added a commit that referenced this pull request Nov 12, 2026
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]>
@marcobambini

Copy link
Copy Markdown
Member

Follow-up: this broke the Windows build after merge, fixed in c8ae85a.

alarm(30) in the new Interrupt test is POSIX only and mingw does not declare it, so test/main.c did not compile on windows-x86_64 at all. My review built on macOS and Linux, so I missed it — CI had never run on the PR itself, which is why nothing flagged it earlier.

Replaced with a watchdog thread, using the usleep() and pthreads the test already relies on there. I checked it still does its job: with the interrupt handler removed it aborts after the timeout with an explicit message instead of hanging.

CI is green on all 15 jobs now. Nothing wrong with the change itself.

@davidschachterADFA

Copy link
Copy Markdown
Contributor Author

Thank you for checking the API guard and for confirming the test fails without the change. Agreed on the ordering: assigning js->db before installing the handler would make it explicit.

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

marcobambini added a commit that referenced this pull request Nov 12, 2026
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]>
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.

2 participants