Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,13 @@ class BackupJob : public ThreadPoolWork {
}

void AfterThreadPoolWork(int status) override {
HandleScope handle_scope(env()->isolate());
Isolate* isolate = env()->isolate();
HandleScope handle_scope(isolate);
Context::Scope context_scope(env()->context());
InternalCallbackScope callback_scope(
env(), Object::New(isolate), {0, 0}, InternalCallbackScope::kNoFlags);
Local<Promise::Resolver> resolver =
Local<Promise::Resolver>::New(env()->isolate(), resolver_);
Local<Promise::Resolver>::New(isolate, resolver_);

if (!(backup_status_ == SQLITE_OK || backup_status_ == SQLITE_DONE ||
backup_status_ == SQLITE_BUSY || backup_status_ == SQLITE_LOCKED)) {
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/sqlite/backup-last-request.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { backup, DatabaseSync } from 'node:sqlite';

const source = new DatabaseSync(':memory:');
source.exec(`
CREATE TABLE data(value);
INSERT INTO data VALUES (zeroblob(1048576));
`);

let keepAlive = setInterval(() => {}, 1_000);
let settled = false;

process.once('beforeExit', () => {
if (!settled) {
process.stderr.write('backup promise did not settle before the event loop became idle\n');
process.exit(1);
}
});

backup(source, process.argv[2], {
rate: 1,
progress() {
if (keepAlive !== undefined) {
clearInterval(keepAlive);
keepAlive = undefined;
}
},
}).then(() => {
settled = true;
source.close();
}, (error) => {
settled = true;
process.stderr.write(`${error.stack ?? error}\n`);
process.exitCode = 1;
});
17 changes: 16 additions & 1 deletion test/parallel/test-sqlite-backup.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Flags: --expose-gc
import { isWindows, skipIfSQLiteMissing } from '../common/index.mjs';
import {
isWindows,
skipIfSQLiteMissing,
spawnPromisified,
} from '../common/index.mjs';
import fixtures from '../common/fixtures.js';
import tmpdir from '../common/tmpdir.js';
import { join } from 'node:path';
import { describe, test } from 'node:test';
Expand Down Expand Up @@ -364,3 +369,13 @@ test('source database is kept alive while a backup is in flight', async (t) => {
const rows = backupDb.prepare('SELECT COUNT(*) AS n FROM data').get();
t.assert.strictEqual(rows.n, 500);
});

test('backup promise settles when the backup is the last active request', async (t) => {
const { code, signal, stderr } = await spawnPromisified(process.execPath, [
fixtures.path('sqlite', 'backup-last-request.mjs'),
nextDb(),
]);

t.assert.strictEqual(signal, null);
t.assert.strictEqual(code, 0, stderr);
});
Loading