diff --git a/Makefile b/Makefile index 7384adb..093d491 100644 --- a/Makefile +++ b/Makefile @@ -124,6 +124,7 @@ ifeq ($(PLATFORM),windows) @echo " sqlite3_js_init" >> $@ @echo " sqlitejs_version" >> $@ @echo " quickjs_version" >> $@ + @echo " sqlitejs_cancel_all" >> $@ endif # Clean up diff --git a/src/sqlitejs.c b/src/sqlitejs.c index 26af849..0a32d36 100644 --- a/src/sqlitejs.c +++ b/src/sqlitejs.c @@ -70,9 +70,12 @@ typedef struct { size_t mem_peak; // the most in use since the last result or error size_t stack_size; // in bytes, 0 for no check: see js_set_max_stack_size() int depth; // calls into JavaScript in progress: see js_enter() + unsigned cancel_epoch; // js_cancel_epoch when the outermost call began + bool check_interrupted; // sqlite3_is_interrupted() is available struct rowset *rowsets; // result sets not yet freed: see js_end_rowsets() int orphans; // of those, the ones whose JavaScript object is gone bool ending_rowsets; // js_end_rowsets() is running + unsigned end_pass; // js_end_rowsets() passes that ended every result set } globaljs_context; typedef struct { @@ -115,6 +118,7 @@ typedef struct rowset { bool ended; // ended by js_end_rowsets(): reading on throws bool interrupted; // and an interrupt is why: reading on throws "interrupted" bool orphaned; // its JavaScript object is gone: see js_rowset_finalizer() + unsigned end_pass; // the js_end_rowsets() pass that ended it struct rowset *prev, *next; // in the connection's list: see js_end_rowsets() } rowset; @@ -270,9 +274,12 @@ static void js_rowset_finalizer(JSRuntime *rt, JSValue val) { // computing, whose js_leave() comes back here and whose garbage collection can free result sets // of the list. So each statement is taken from its result set before it is finalized, the list // is walked again from its head after each one, and a nested call does nothing. +static unsigned js_cancel_epoch_load (void); + static void js_end_rowsets (globaljs_context *js, bool all, bool interrupted) { if (js->ending_rowsets) return; js->ending_rowsets = true; + unsigned pass = all ? ++js->end_pass : 0; for (;;) { rowset *rs = js->rowsets; while (rs && !(rs->orphaned || (all && rs->vm))) rs = rs->next; @@ -289,10 +296,17 @@ static void js_end_rowsets (globaljs_context *js, bool all, bool interrupted) { } else { rs->vm = NULL; rs->ended = true; + rs->end_pass = pass; if (interrupted) rs->interrupted = true; } if (vm) sqlite3_finalize(vm); } + // A cancel that landed while the finals ran stopped them, as one before the call returned + // would have: the result sets this pass ended read on as interrupted too. Nothing runs + // JavaScript here, so the list holds only result sets that are still allocated. + if (all && !interrupted && (js->cancel_epoch != js_cancel_epoch_load())) { + for (rowset *r = js->rowsets; r; r = r->next) if (r->ended && (r->end_pass == pass)) r->interrupted = true; + } js->ending_rowsets = false; } @@ -325,6 +339,30 @@ static void js_collect_below_limit (globaljs_context *js) { js_keep_gc_below_limit(js); } +// Bumped by sqlitejs_cancel_all(): JavaScript that began before stops, as if interrupted. +static unsigned js_cancel_epoch; + +static unsigned js_cancel_epoch_load (void) { +#if defined(__clang__) || defined(__GNUC__) + return __atomic_load_n(&js_cancel_epoch, __ATOMIC_ACQUIRE); +#else + return *(volatile unsigned *)&js_cancel_epoch; +#endif +} + +// Stops all JavaScript that is running now, in every connection: each call fails with an +// "interrupted" error, and the statement that called it is interrupted. JavaScript that begins +// afterwards runs normally. Safe from any thread. +// For hosts whose own cancelling cannot reach JavaScript: Android cancels a query with a +// progress handler, which SQLite never calls while a JavaScript function runs. +void sqlitejs_cancel_all (void) { +#if defined(__clang__) || defined(__GNUC__) + __atomic_add_fetch(&js_cancel_epoch, 1, __ATOMIC_RELEASE); +#else + (*(volatile unsigned *)&js_cancel_epoch)++; +#endif +} + // QuickJS calls this from time to time while it runs code. A non-zero result stops the code // with an uncatchable "interrupted" error, so sqlite3_interrupt() (Ctrl-C in the sqlite3 shell, // a cancelled query in an app) also stops JavaScript that never returns to SQLite. @@ -332,7 +370,17 @@ static int js_interrupt_handler (JSRuntime *rt, void *opaque) { UNUSED(rt); globaljs_context *js = (globaljs_context *)opaque; js_keep_gc_below_limit(js); - return (js->db != NULL) && sqlite3_is_interrupted(js->db); + // Only a call (see js_enter) is cancelled. The one exception is the JavaScript the extension + // itself runs while it initialises a connection (globaljs_init, depth 0): stopping it would + // make the connection fail to load. Stored functions loaded by js_init_table run as a call, + // so they can be cancelled. + if ((js->depth > 0) && (js->cancel_epoch != js_cancel_epoch_load())) { + // The statement is interrupted too: a collation cannot report an error, so the + // statement would go on calling JavaScript, each call beginning after the cancel. + if (js->db) sqlite3_interrupt(js->db); + return 1; + } + return js->check_interrupted && (js->db != NULL) && sqlite3_is_interrupted(js->db); } // The most memory, in bytes, the JavaScript runtime of a connection may allocate until @@ -542,6 +590,10 @@ static bool js_stack_room (size_t *room) { // runs JavaScript) is on the same thread and keeps the outer call's limit. static void js_enter (globaljs_context *js) { if (js->depth++ > 0) return; + // The finals that ending a call's result sets runs (see js_leave) belong to that call: they keep + // its epoch, so a cancel during the call or during the ending stops them, as it would have + // stopped the call. Before SQLite 3.41 nothing else would stop one that never ends. + if (!js->ending_rowsets) js->cancel_epoch = js_cancel_epoch_load(); JS_UpdateStackTop(js->runtime); // A thread with less room than SQLITE_JS_STACK_MIN keeps it too: JavaScript there then fails // with "Maximum call stack size exceeded" rather than running past the end of the stack. A @@ -556,6 +608,8 @@ static void js_enter (globaljs_context *js) { static void js_leave (globaljs_context *js) { if (--js->depth > 0) return; if (!js->rowsets) return; + // A cancel (sqlitejs_cancel_all) is not seen here: js_end_rowsets() marks its result sets + // interrupted itself, which is the only way they read so before SQLite 3.41. bool interrupted = (sqlite3_libversion_number() >= 3041000) && js->db && sqlite3_is_interrupted(js->db); js_end_rowsets(js, true, interrupted); } @@ -577,8 +631,9 @@ static globaljs_context *globaljs_init (sqlite3 *db) { //JS_SetMaxStackSize(rt, 10 * 1024); // sqlite3_is_interrupted() is new in SQLite 3.41.0: an older library has no such entry - // in its sqlite3_api_routines, so JavaScript cannot be interrupted there - if (sqlite3_libversion_number() >= 3041000) JS_SetInterruptHandler(rt, js_interrupt_handler, js); + // in its sqlite3_api_routines, so JavaScript cannot be interrupted there, only cancelled + js->check_interrupted = (sqlite3_libversion_number() >= 3041000); + JS_SetInterruptHandler(rt, js_interrupt_handler, js); // the default stack size must fit in the stack of the thread that loads the extension: Windows // gives a main thread 1 MB, and apps often give their threads no more @@ -900,6 +955,7 @@ static JSValue js_sqlite_exec (JSContext *ctx, sqlite3 *db, const char *sql, int rs->vm = vm; rs->ncols = sqlite3_column_count(vm); rs->ended = false; + rs->end_pass = 0; rs->interrupted = false; rs->orphaned = false; rs->prev = rs->next = NULL; diff --git a/src/sqlitejs.h b/src/sqlitejs.h index fb0172d..7cf1ce3 100644 --- a/src/sqlitejs.h +++ b/src/sqlitejs.h @@ -21,5 +21,8 @@ int sqlite3_js_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi); const char *sqlitejs_version (void); const char *quickjs_version (void); +// Stops all JavaScript now running, in every connection. Safe to call from any thread. It +// stops only JavaScript already running: calls that begin afterwards are not affected. +void sqlitejs_cancel_all (void); #endif diff --git a/test/main.c b/test/main.c index 8cc3706..d670bf9 100644 --- a/test/main.c +++ b/test/main.c @@ -1047,6 +1047,149 @@ int test_memory_limit (void) { return rc; } +// MARK: - Cancel all - + +// Set by started(), an SQL function that the JavaScript under test calls before it spins, so +// that the worker cancels only JavaScript that is running: a cancel that came first would not +// stop it +static int test_js_started; + +static void test_started (sqlite3_context *context, int argc, sqlite3_value **argv) { + (void)argc; (void)argv; + __atomic_store_n(&test_js_started, 1, __ATOMIC_RELEASE); + sqlite3_result_null(context); +} + +// Cancels from inside the statement under test: the cancel lands before the call returns, but +// after its JavaScript last checked for one +static void test_cancel_now (sqlite3_context *context, int argc, sqlite3_value **argv) { + (void)argc; (void)argv; + sqlitejs_cancel_all(); + sqlite3_result_null(context); +} + +static void *test_cancel_all_worker (void *ptr) { + (void)ptr; + // wait for the JavaScript to start, for at most 10 s; cancelling before it starts would + // leave it running, so a JavaScript that has not started by then fails the test here + for (int i = 0; i < 1000 && !__atomic_load_n(&test_js_started, __ATOMIC_ACQUIRE); ++i) usleep(10 * 1000); + if (!__atomic_load_n(&test_js_started, __ATOMIC_ACQUIRE)) { + printf("FAIL: the JavaScript under test did not start in 10 seconds\n"); + fflush(stdout); + abort(); + } + sqlitejs_cancel_all(); + return NULL; +} + +static int test_cancelling; + +static void *test_cancel_loop (void *ptr) { + (void)ptr; + while (__atomic_load_n(&test_cancelling, __ATOMIC_ACQUIRE)) sqlitejs_cancel_all(); + return NULL; +} + +// Opens connections while another thread cancels without pause. Setting up a connection runs +// JavaScript of its own, and a cancel that stopped it made the connection fail to load. +static int test_cancel_during_open (void) { + pthread_t thread; + __atomic_store_n(&test_cancelling, 1, __ATOMIC_RELEASE); + if (pthread_create(&thread, NULL, test_cancel_loop, NULL) != 0) return SQLITE_ERROR; + int rc = SQLITE_OK; + for (int i = 0; (i < 200) && (rc == SQLITE_OK); ++i) { + sqlite3 *db = NULL; + rc = test_open(&db); + if (rc != SQLITE_OK) printf("FAIL: connection %d did not open while another thread cancelled: %s\n", i, db ? sqlite3_errmsg(db) : sqlite3_errstr(rc)); + if (db) sqlite3_close(db); + } + __atomic_store_n(&test_cancelling, 0, __ATOMIC_RELEASE); + pthread_join(thread, NULL); + return rc; +} + +// sqlitejs_cancel_all() stops the JavaScript running in every connection, as Android's own +// cancelling (a progress handler, which SQLite never calls while a function runs) cannot. +int test_cancel_all (void) { + sqlite3 *db = NULL, *db2 = NULL; + pthread_t thread; + + int seconds = 30; + pthread_t watchdog; + test_watchdog_stop = 0; + bool watching = (pthread_create(&watchdog, NULL, test_watchdog, &seconds) == 0); + + int rc = test_open(&db); + if (rc != SQLITE_OK) goto abort_test; + rc = sqlite3_create_function(db, "started", 0, SQLITE_UTF8, NULL, test_started, NULL, NULL); + if (rc != SQLITE_OK) goto abort_test; + rc = db_exec(db, "SELECT js_create_scalar('spin', '(function (args) { db.exec(\"SELECT started()\").next(); for (;;) { try { while (true) {} } catch (e) {} } })');"); + if (rc != SQLITE_OK) goto abort_test; + + // JavaScript running now stops, even code that catches every exception + test_js_started = 0; + if (pthread_create(&thread, NULL, test_cancel_all_worker, NULL) != 0) { rc = SQLITE_ERROR; goto abort_test; } + rc = check_error(db, "SELECT spin();", "interrupted"); + pthread_join(thread, NULL); + if (rc != SQLITE_OK) goto abort_test; + + // a collation cannot report an error, so its statement is interrupted too: otherwise the sort + // would go on calling it, and each call, begun after the cancel, would run + rc = db_exec(db, "SELECT js_create_collation('spin_collation', '(function (a, b) { db.exec(\"SELECT started()\").next(); for (;;) {} })');"); + if (rc != SQLITE_OK) goto abort_test; + test_js_started = 0; + if (pthread_create(&thread, NULL, test_cancel_all_worker, NULL) != 0) { rc = SQLITE_ERROR; goto abort_test; } + rc = check_error(db, "SELECT column1 FROM (VALUES ('a'), ('b'), ('c')) ORDER BY column1 COLLATE spin_collation;", "interrupted"); + pthread_join(thread, NULL); + if (rc != SQLITE_OK) goto abort_test; + + // When a call returns, ending its result sets runs the finals of the window functions they + // were computing, and those finals belong to the call. A cancel that lands while they are + // ended stops every one, and the result sets read on as interrupted: here the newer result + // set's final starts the cancel and never ends, then the older one's never ends. + rc = db_exec(db, "SELECT js_create_window('loop_final', 'total = 0;', '(function (args) { total += args[0]; })', '(function () { while (true) {} })', '(function () { return total; })', '(function (args) { total -= args[0]; })');"); + if (rc != SQLITE_OK) goto abort_test; + rc = db_exec(db, "SELECT js_create_window('start_final', 'total = 0;', '(function (args) { total += args[0]; })', '(function () { db.exec(\"SELECT started()\").next(); while (true) {} })', '(function () { return total; })', '(function (args) { total -= args[0]; })');"); + if (rc != SQLITE_OK) goto abort_test; + test_js_started = 0; + if (pthread_create(&thread, NULL, test_cancel_all_worker, NULL) != 0) { rc = SQLITE_ERROR; goto abort_test; } + rc = check_error(db, "SELECT js_eval('globalThis.a = db.exec(\"SELECT loop_final(column1) OVER (ORDER BY column1) FROM (VALUES (1), (2), (3))\"); a.next(); globalThis.b = db.exec(\"SELECT start_final(column1) OVER (ORDER BY column1) FROM (VALUES (1), (2), (3))\"); b.next(); 0');", "interrupted"); + pthread_join(thread, NULL); + if (rc != SQLITE_OK) goto abort_test; + rc = check_query(db, "SELECT js_eval('[a, b].map((r) => { try { r.next(); return \"open\"; } catch (e) { return e.message; } }).join()');", "interrupted,interrupted"); + if (rc != SQLITE_OK) goto abort_test; + + // A cancel that lands as a call returns, after its JavaScript last checked for one, stops the + // final that ending its result set runs, and the call fails rather than skipping the final + rc = sqlite3_create_function(db, "cancel_now", 0, SQLITE_UTF8, NULL, test_cancel_now, NULL, NULL); + if (rc != SQLITE_OK) goto abort_test; + rc = db_exec(db, "CREATE TABLE ran(x); SELECT js_create_window('long_final', 'total = 0;', '(function (args) { total += args[0]; })', '(function () { let s = 0; for (let i = 0; i < 20000000; i++) s += i; db.exec(\"INSERT INTO ran VALUES (1)\").next(); return total; })', '(function () { return total; })', '(function (args) { total -= args[0]; })');"); + if (rc != SQLITE_OK) goto abort_test; + rc = check_error(db, "SELECT js_eval('globalThis.c = db.exec(\"SELECT long_final(column1) OVER (ORDER BY column1) FROM (VALUES (1), (2), (3))\"); c.next(); db.exec(\"SELECT cancel_now()\").next(); 0');", "interrupted"); + if (rc != SQLITE_OK) goto abort_test; + rc = check_query(db, "SELECT count(*) FROM ran;", "0"); + if (rc != SQLITE_OK) goto abort_test; + + // JavaScript that begins afterwards runs normally, on this connection and on a new one + rc = check_query(db, "SELECT js_eval('1 + 1');", "2"); + if (rc != SQLITE_OK) goto abort_test; + rc = test_open(&db2); + if (rc != SQLITE_OK) goto abort_test; + rc = check_query(db2, "SELECT js_eval('2 + 2');", "4"); + if (rc != SQLITE_OK) goto abort_test; + + // and a connection opened while cancels go on still loads the extension + rc = test_cancel_during_open(); + +abort_test: + test_watchdog_stop = 1; + if (watching) pthread_join(watchdog, NULL); + if (rc != SQLITE_OK && db) printf("Error: %s\n", sqlite3_errmsg(db)); + if (db) sqlite3_close(db); + if (db2) sqlite3_close(db2); + return rc; +} + // MARK: - Stack size - int test_stack_size (void) { @@ -1276,6 +1419,7 @@ int main (void) { result += test_report("Serialization 3", test_serialization(DB_PATH, true, 3)); // load the new implementations result += test_report("Thread", test_thread()); result += test_report("Interrupt", test_interrupt()); + result += test_report("Cancel all", test_cancel_all()); result += test_report("Direct only", test_direct_only()); result += test_report("Aggregate errors", test_aggregate_errors()); result += test_report("Exec errors", test_exec_errors());