Skip to content

ADFA-7081: Bring sqlite-js to main (#6 merged into #5's branch after #5 merged) - #7

Merged
davidschachterADFA merged 17 commits into
mainfrom
feat/ADFA-7081-sqlite-js
Nov 17, 2026
Merged

davidschachterADFA merged 17 commits into
mainfrom
feat/ADFA-7081-sqlite-js

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

ADFA-7081 (phase 1 of ADFA-929)

This brings sqlite-js to main. #6 added it and was approved, but it was stacked on #5. #5 merged its branch, feat/ADFA-6635-sqlite-brotli, into main at 21:00:07Z on 2026-11-16, and #6 merged into that same branch at 21:00:43Z. So #6's commits never reached main. GitHub moves a PR to main only when its base branch is deleted, and that branch wasn't.

This PR's branch is #6's head, fbc3be3 (sqlite-js 1.4.2-3), with no changes. It adds exactly what #6 added, and no new code:

  • packages/sqlite-js/: build.sh, sqlite-js.map, README.md, tests/run-tests.sh;
  • the sqlite-js row in README.md's package table;
  • the sqlite-js entry in packages.sh's COTG_PACKAGES.

That's 6 files, +1124 lines, with nothing removed.

Merged and published. This PR merged on 2026-11-17 as 96e4b0e. Merging publishes nothing by itself, because build-packages and copy-prod-to-stage run only when started by hand. Run 44014027669 (build-packages on main at 96e4b0e) then published sqlite-js 1.4.2-3 and sqlite-brotli 1.0.0-2 to production, packages.appdevforall.org, for aarch64 and arm.

Testing (1.4.2-3):

  • On the host, 104 tests run: 103 pass and AC12 is skipped (no sqlite-brotli).
  • On a Galaxy Note20 Ultra, upgraded through apt from stage, all 105 tests pass on aarch64 and on 32-bit arm. The 50,000,000-row runaway takes 26 s there, so it runs long enough to be stopped.
  • ADFA-7081's Steps to QA, installed from production, pass on aarch64 and in Code on the Go's 32-bit build.
  • On a Galaxy J7 (Android 9, 32-bit), production's 1.4.2-3 fails test 58, "AC9: memory held by a global counts until the global is cleared": the 32-bit allocator rounds its 60 MB block up to the whole 64 MB limit. The test is at fault; ADFA-7200: Build sqlite-js 1.4.4, which has upstream #36 #8 holds 48 MB instead.

For the full description and the test details, see #6.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw

davidschachterADFA and others added 17 commits November 10, 2026 21:53
…ite3

sqlite-js packages sqliteai/sqlite-js 1.3.7, a SQLite loadable
extension for scalar, aggregate, window and collation functions written
in JavaScript, with quickjs-ng 0.12.1 built in. It installs
$PREFIX/lib/sqlite-js.so, loaded with `.load sqlite-js`, plus a README
and acceptance tests.

Six patches, each to be sent upstream, fix what 1.3.7 gets wrong:
- 0001: sqlite3_interrupt() (Ctrl-C in sqlite3) stops running
  JavaScript. Before, `while (true) {}` hung the shell.
- 0002: a 64 MB memory limit per connection, and js_set_memory_limit().
  Before, code that kept allocating took all the memory there was.
- 0003: js_eval() and the other functions that run code, read files or
  change the runtime are SQLITE_DIRECTONLY, so a database's views and
  triggers cannot call them even with trusted_schema on.
- 0004: an ArrayBuffer or typed array result is a BLOB. Before, it was
  NULL, so no BLOB could pass through a function.
- 0005: a whole-number result in the safe-integer range is an INTEGER.
  Before, an INTEGER outside the 32-bit range came back as a REAL.
- 0006: an exception in an aggregate step or window inverse function is
  an SQL error. Before, it was ignored and the result silently wrong.
Each adds a test to upstream's test/main.c, which fails without it.

build.sh exports only the entry points (sqlite-js.map), since SQLite
opens extensions with RTLD_GLOBAL, and adds sqlite3_sqlitejs_init, the
name SQLite derives from the file name, as an alias of upstream's
sqlite3_js_init. It compiles against Termux's SQLite headers, putting
the copies bundled in libs/ last with -idirafter, and collects QuickJS's
MIT license, which is only in source comments, into LICENSE.quickjs.

QuickJS's std and os modules stay: JavaScript cannot reach them, since
nothing runs its job queue, so import() never completes. A test checks
that it stays so.

Tests: upstream's test/main.c passes 12 of 12, and its six new tests
fail on 1.3.7. tests/run-tests.sh passes 73 of 73 on x86_64 Linux with
sqlite3 3.46.1, and 25 fail against unpatched 1.3.7; it runs the
README's sql examples as written. The recipe's steps run outside Docker
on the host and with the NDK r29 toolchain at API 24 for aarch64 and
arm: six patches apply, only the two entry points are exported, and the
libraries need only libc, libm and libdl. Termux's linter passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
An independent Claude review of 500e38e found two bugs in the patches,
gaps in the tests, and two older problems. The package goes to 1.3.7-1.

- 0004: a typed array that tracks the length of a resizable buffer kept
  the length QuickJS recorded when it was made. After the buffer shrank,
  returning or binding the array read past the buffer: a 32 MB view of a
  buffer shrunk to 1 byte crashed sqlite3. The offset and length now come
  from the byteOffset and byteLength getters, read before the bytes, and
  are checked against the buffer's size.
- 0002: QuickJS can crash when it runs out of memory while it creates a
  context, which every aggregate does, so with the new limit an
  aggregate run with little memory left crashed sqlite3. Contexts are now
  created with the limit lifted; it applies again to the code that runs
  in them, and is set for the connection once its own context exists.
- 0003: the SQLITE_DIRECTONLY flag is computed from the function's name,
  not from an array that parallels the function list, which a function
  added later would also have to extend.
- run-tests.sh: without sqlite-brotli or script(1), the tests that need
  them are skipped rather than failed, and the summary says so. AC9
  checks each statement's output and bounds the default limit (48 MB
  fits, 70 MB does not); a 32 MB default passed before. AC10 checks the
  SQLITE_DIRECTONLY flag of every function in function_list; a missing
  flag on js_create_* passed before. New: typed arrays over resized
  buffers (AC6) and aggregates near the memory limit (AC9).
- README: a collation that throws or runs out of memory compares as
  equal, so ORDER BY gives a wrong order without an error, as upstream
  intends.

Not changed: QuickJS records the stack top of the thread that loads the
extension, so JavaScript run on that connection from another thread
fails with "Maximum call stack size exceeded". The sqlite3 shell has one
thread; this matters for apps (ADFA-7082).

Tests: upstream's test/main.c passes 12 of 12, also under ASan and UBSan
with leak checking, and its new checks fail against the previous
patches. run-tests.sh passes 77 of 77 on x86_64 Linux, also with the
extension under ASan and UBSan; against the 1.3.7 build, the two new
tests fail; without sqlite-brotli, 76 pass and 1 is skipped. The NDK
builds for aarch64 and arm export only the entry points, and Termux's
linter passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
… stack

Patch 0007: js_set_max_stack_size() passed its argument to QuickJS
unchecked, so text (read as 0, which turns the check off) or a size
larger than the thread's stack made endless recursion crash sqlite3,
and the 1 MB default did the same on a thread with less stack. It now
measures the calling thread's stack, keeps a 256 KB margin, refuses
text, negatives and sizes beyond what the stack allows (the error names
the largest size), and lowers the default when the loading thread has
less. 0 still turns the check off, as upstream documents for use from
several threads.

run-tests.sh: AC13 checks the error cases, endless recursion at the
default, just below the largest size (also through db.exec) and with a
512 KB stack. README lists the seventh patch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Patch 0008 (F20): db.exec's result set treated any sqlite3_step()
result but SQLITE_ROW as the end of the rows, so an error while the
statement ran was dropped: next() returned false and toArray() the rows
before the error, and a failed write looked like success. Both now
throw the connection's error message; the end of the rows is still
false. The README documents db.exec, including that a statement runs
only when next() or toArray() steps it (F21).

Patch 0007: its SQL-recursion test catches the deeper call's error
itself, so it passes with or without 0008 (only the test changed).

run-tests.sh: new AC14 for the error cases; AC13's recursion through
db.exec likewise. 91 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
…processes

The interactive Ctrl-C test ran js_eval('while (true) {}') under
timeout 30 script(1). Against a build where Ctrl-C does not stop
JavaScript, such as unpatched 1.3.7, timeout stopped script but not the
sqlite3 it ran, which kept a CPU core busy until killed; three were found
after 17 hours on a development machine.

Each runaway loop in the AC8 tests now gives up after 20 s, and the
interactive test must finish within 15 s, since the shell also reports
"interrupted" when a loop ends by itself. The AC9 allocation loops stop
at about 4 times the 64 MB default, so a build without the limit fails
the test instead of exhausting the device's memory. timeout also kills
script if it ignores SIGTERM.

Tested on x86_64 Linux: the 1.3.7-3 build passes 91 of 91. Unpatched
1.3.7 fails 39, including the four Ctrl-C tests and the allocation-loop
test, and leaves no process or test directory behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
From the independent review of PR #6:
- 0002: QuickJS sets its garbage-collection threshold to 1.5 times the
  memory in use. Past two thirds of the limit, that is beyond the limit,
  so garbage filled it and code that fits failed. The threshold is now
  kept between the memory in use and the limit.
- 0002: running out of memory through small allocations leaves no room
  for QuickJS's error, so QuickJS throws null. That now reports "out of
  memory" instead of "Unknown JavaScript exception".
- 0004: db.exec throws for a buffer it cannot read (detached, or a
  typed array out of its buffer's bounds) instead of binding NULL.
- 0004, from CodeRabbit on CodeOnTheGo #2244: a typed array's offset and
  length are read through getters taken when the runtime is made, so
  properties a script defines over them cannot change which bytes are
  read.
- 0003 and the README: TEMP views and triggers can call the js_*
  functions, since SQLite trusts the TEMP schema.

Patches 0001-0008 are regenerated from the work repo. Only 0002, 0003
and 0004 change in substance. The others change in their headers, hunk
line numbers and index lines.

run-tests.sh adds six checks, for 97 in all:
- garbage is collected with 48 of 64 MB held;
- an aggregate over 300 groups runs with 48 of 64 MB held;
- small allocations run out with "out of memory";
- db.exec throws for buffers it cannot bind;
- shadowed typed-array properties are ignored;
- a TEMP view can call js_eval.

On a host build, 97 of 97 pass, and 1.3.7-4 fails the five that test
the fixes. Upstream test/main.c passes 14 of 14 under ASan and UBSan
with leak checking, and each new test there fails without its fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
…merged

Upstream merged our eight patches (sqliteai/sqlite-js #11 to #18) and #19
on 2026-11-12, with fixes of its own. One fixes a bug in our 0002, which
1.3.7-5 has: a memory limit lowered under memory that is garbage left the
session out of memory until the limit was raised again (ef06558).

Build from upstream main at c8ae85a, with no patches. The version follows
termux-packages' convention for a commit after a release, and dpkg sorts
1.3.7-p20261112 above 1.3.7-5. The README now lists the upstream PRs
instead of patches and says what a lowered limit does.

run-tests.sh gets a test for that bug: 98 of 98 pass on a host build of
this package, and on 1.3.7-5 only the new test fails. The aarch64 and arm
builds (emulated build.sh steps, NDK 29) export only the two entry points.
ADFA-7083's page examples pass, 64 of 64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Upstream released 1.4.0 and 1.4.1 on 2026-11-12, after c8ae85a. Both
contain it, and 1.4.1 differs from it in src/ and libs/ only in
SQLITE_JS_VERSION, so js_version() now reports 1.4.1. build.sh takes
the 1.4.1 tag again, as its comment asked (independent review).

Two patches, offered upstream as sqliteai/sqlite-js #22 and #23:
- 0001 keeps the stack check within the stack where endless recursion
  crashed: a thread with less than 64 KB of stack left, and a main
  thread first measured deep in its stack (CodeRabbit, on the AAR's copy
  in CodeOnTheGo #2244). Neither can happen in sqlite3; the README's
  stack claim now holds everywhere.
- 0002 collects garbage after any "out of memory" error and whenever a
  limit is set. With 40 MB of cyclic garbage under the 64 MB default, a
  30 MB ArrayBuffer failed on every try. After a limit lowered to 32 MB,
  above the garbage, a 16 MB one failed (independent review, both).

The README describes both. Its js_set_max_stack_size row no longer
suggests 0 for several threads, which upstream's #19 made unneeded
(independent review). run-tests.sh gets an AC9 test for each garbage
case, and AC2 expects 1.4.1.

On a host build of this package, run-tests.sh passes 99 of 99, with
AC12 skipped because sqlite-brotli is not installed. On 1.4.1 without
the patches, only the two new tests fail. Upstream's suite with the
patches passes 15 of 15, also under ASan and UBSan. The aarch64 and arm
builds (NDK 29, build.sh's compile line) have no warnings in sqlitejs.c,
and export only the two entry points.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
SQLite keeps an interrupt in effect until no statement of the connection
is running, and a db.exec result set that JavaScript keeps open is a
running statement. In 1.4.1, Ctrl-C during code that keeps one, in a
global say, left every later statement in the sqlite3 session failing
with "interrupted" until it was restarted. CodeRabbit found it on the
AAR's copy (CodeOnTheGo #2244).

Patches 0003 and 0004, offered upstream as sqliteai/sqlite-js #24: when
the outermost call into JavaScript returns on an interrupted connection,
the result sets still open are finalized, and reading on from a kept one
fails with "interrupted". 0004 makes that safe when finalizing runs a
JavaScript window or aggregate function's final. The README says so.

run-tests.sh's interactive Ctrl-C test is now a function, and a second
case keeps a result set open: Ctrl-C, then SELECT 'still' || ' alive'.
On a host build of this package, run-tests.sh passes 100 of 100, with
AC12 skipped (no sqlite-brotli). With only patches 0001 and 0002, only
the new test fails. Upstream's suite with the four patches passes 15 of
15, also under ASan and UBSan. The aarch64 and arm builds (NDK 29,
build.sh's compile line) have no warnings in sqlitejs.c and export only
the two entry points.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
An interrupt ends every db.exec result set still open in the session,
not only the stopped code's (independent review of the AAR's copy,
CodeOnTheGo #2244). And Ctrl-C while no JavaScript runs, during plain
SQL, leaves the session failing every statement while JavaScript keeps a
result set open, since no JavaScript runs to end it: reproduced on
SQLite 3.49, where SELECT 1 + 2 and js_eval() then fail with
"interrupted". The README says so, and to finish result sets before the
code returns. README only: the stage packages of 1.4.1 (CI run
43355418863) have the earlier wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
Upstream merged sqliteai/sqlite-js #22, #23 and #24 and released them
in 1.4.2; the four patches have the same patch ids as upstream commits
ac17d4f, 7776629, 28fa507 and 347ee9d, so they are dropped. The
maintainer added two commits to #22: the main thread's stack is
measured from the kernel's [stack] mapping, and with no stack limit
(ulimit -s unlimited) js_set_max_stack_size allows at most 1 MB.

1.4.2 also has upstream's fix for our report #25 (#26): a db.exec
result set ends when the call into JavaScript that created it returns,
and reading it later throws "The result set was closed when the call
into JavaScript that created it returned". So a result set kept in a
global no longer keeps a statement open, and Ctrl-C during plain SQL no
longer leaves the session failing every statement.

run-tests.sh: AC2 expects 1.4.2; two new tests, one for each behaviour
above. On the host (sqlite3 3.46.1) plain 1.4.2 passes 102 of 102; the
1.4.1 build with the old patches fails both new tests for the reasons
they name. README: the release, the result-set rule, the stack limit,
1.4.2's changes, and no patches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
From the independent review of 86b48e5 (nothing blocking; three low
findings, three nits), and the review of the ADFA-7083 pages:
- README: a result set made in a call that SQL in another call's
  db.exec ran lasts until the outer call returns. Read later, it throws
  "interrupted" if Ctrl-C stopped its call, and one whose next()
  returned false or whose toArray() ran just reports no more rows (F01).
  #24's row again says reading on fails with "interrupted".
- README: four tests need other packages, not two (F02); #22's row
  names both of upstream's additions (F04); a long line rewrapped (F05).
- run-tests.sh: AC13 checks that with no stack limit sizes above 1 MB
  are refused, as the README says, and skips where `ulimit -s
  unlimited` is not allowed (F03).

Corrections to 86b48e5's message (F06): the maintainer's two commits on
#22 are bd3d6c4, which skips upstream's 64 KB-thread test where the
platform has no stack that small, and 31ed714, which has both changes
named there; and of its "102 of 102", one test (AC12) was skipped.

On the host (sqlite3 3.46.1, no sqlite-brotli): 1.4.2 passes 103 of 103
with AC12 skipped; the 1.4.1 build fails the new AC13 test, AC2 and the
two tests 86b48e5 added, each for the reason it names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
70f2643 changed run-tests.sh and the README, which the package installs,
but not the version, so apt on a phone with 1.4.2 would keep the old
files. Revision 1 makes it 1.4.2-1, which dpkg sorts above 1.4.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
From review 5918442139 of f882b35 (MINOR and NITPICK only):
- Ctrl-C tests: expect_sigint_stops slept 1 s and
  expect_ctrl_c_keeps_session 2 s before the signal, so on a slow start
  it could arrive before the runaway statement ran and be lost. Each
  runaway statement now creates the file "started" as it begins
  (WHERE writefile('started', '') IS NOT NULL), and the helpers wait
  for it, up to 10 s. The file is made inside the statement because
  SQLite drops an interrupt that arrives between statements.
- The plain-SQL runaway counted to 2,000,000,000, minutes past the
  20 s cap the other runaways keep; it counts to 200,000,000 (about
  18 s on the host).
- AC4 "stores the new code" checked only the session that redefined
  the function. A new test loads js_functions in a fresh session
  (js_init_table(1)) and expects the new code's result.
- README: sqlite3 keeps running after Ctrl-C only in an interactive
  session; from a file or arguments it exits. The repository README no
  longer says the package has patches.
- AC10: the view block's comment says what it checks.

Host (sqlite3 3.46.1): 104 tests, all pass but AC12, skipped (no
sqlite-brotli); 103 before. The SIGINT tests now take 0.2 s, not 1.2. With a sqlite3 wrapper that starts 4 s late, all six AC8
tests failed before and pass now. Against a js_functions row that kept
the old code, the new AC4 test fails (42, not 63).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
The package installs run-tests.sh and the README, which the previous
commit changed, so the revision goes up for apt to install them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
- A runaway statement that has not started within 10 s now fails its
  AC8 test as such ("the statement did not start within 10 s"), in
  both helpers, instead of sending Ctrl-C anyway and blaming the build.
  The batch helper also stops waiting once sqlite3 has exited. With a
  sqlite3 that starts 12 s late, all six AC8 tests report that; the
  old tests reported "still running 5 s after SIGINT" or "took 13 s".
- The plain-SQL runaway counts to 50,000,000, not 200,000,000: about
  5 s on an x86_64 host (measured), so a lost Ctrl-C leaves sqlite3
  busy for about as long as the other runaways' 20 s cap on a phone.

Host: passed all 104, AC12 skipped (no sqlite-brotli).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
The package installs run-tests.sh, so the test changes need a new
revision for the publish to pick them up. The library is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw
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.

1 participant