sqlite: keep source database alive during backup

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/62673
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-06-18 00:15:50 +02:00
1 parent 7d81419cf2
commit 30e71c7e49
2 files changed
+54 -3

No files matched your search

+13 -3
View File
@@ -565,8 +565,10 @@ class BackupJob : public ThreadPoolWork {
TryCatch try_catch(env()->isolate());
USE(fn->Call(env()->context(), Null(env()->isolate()), 1, argv));
if (try_catch.HasCaught()) {
Local<Value> exception = try_catch.Exception();
Finalize();
resolver->Reject(env()->context(), try_catch.Exception()).ToChecked();
resolver->Reject(env()->context(), exception).ToChecked();
delete this;
return;
}
}
@@ -585,11 +587,15 @@ class BackupJob : public ThreadPoolWork {
resolver
->Resolve(env()->context(), Integer::New(env()->isolate(), total_pages))
.ToChecked();
delete this;
}
void Finalize() {
Cleanup();
source_->RemoveBackup(this);
if (source_) {
source_->RemoveBackup(this);
source_.reset();
}
}
void Cleanup() {
@@ -610,28 +616,32 @@ class BackupJob : public ThreadPoolWork {
Local<Object> e;
if (!CreateSQLiteError(env()->isolate(), dest_).ToLocal(&e)) {
Finalize();
delete this;
return;
}
Finalize();
resolver->Reject(env()->context(), e).ToChecked();
delete this;
}
void HandleBackupError(Local<Promise::Resolver> resolver, int errcode) {
Local<Object> e;
if (!CreateSQLiteError(env()->isolate(), errcode).ToLocal(&e)) {
Finalize();
delete this;
return;
}
Finalize();
resolver->Reject(env()->context(), e).ToChecked();
delete this;
}
Environment* env() const { return env_; }
Environment* env_;
DatabaseSync* source_;
BaseObjectPtr<DatabaseSync> source_;
Global<Promise::Resolver> resolver_;
Global<Function> progressFunc_;
sqlite3* dest_ = nullptr;
+41
View File
@@ -1,3 +1,4 @@
// Flags: --expose-gc
import { isWindows, skipIfSQLiteMissing } from '../common/index.mjs';
import tmpdir from '../common/tmpdir.js';
import { join } from 'node:path';
@@ -314,3 +315,43 @@ test('backup has correct name and length', (t) => {
t.assert.strictEqual(backup.name, 'backup');
t.assert.strictEqual(backup.length, 2);
});
test('source database is kept alive while a backup is in flight', async (t) => {
// Regression test: previously, BackupJob stored a raw DatabaseSync* and the
// source could be garbage-collected while the backup was still running,
// leading to a use-after-free when BackupJob::Finalize() dereferenced the
// stale pointer via source_->RemoveBackup(this).
const destDb = nextDb();
let database = makeSourceDb();
// Insert enough rows to ensure the backup takes multiple steps.
const insert = database.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
for (let i = 3; i <= 500; i++) {
insert.run(i, 'A'.repeat(1024) + i);
}
const p = backup(database, destDb, {
rate: 1,
progress() {},
});
// Drop the last strong JS reference to the source database. With the bug,
// the DatabaseSync could be collected here and the in-flight backup would
// later crash while accessing the freed source.
database = null;
// Nudge the GC aggressively, but the backup must keep the source alive
// regardless. Without the fix, the source DatabaseSync would be collected
// and BackupJob::Finalize() would crash the process.
for (let i = 0; i < 5; i++) {
global.gc();
await new Promise((resolve) => setImmediate(resolve));
}
const totalPages = await p;
t.assert.ok(totalPages > 0);
const backupDb = new DatabaseSync(destDb);
t.after(() => { backupDb.close(); });
const rows = backupDb.prepare('SELECT COUNT(*) AS n FROM data').get();
t.assert.strictEqual(rows.n, 500);
});