sqlite: run backup completion in callback scope · nodejs/node@b7b18fd · GitHub
Skip to content

Commit b7b18fd

Browse files
panvaaduh95
authored andcommitted
sqlite: run backup completion in callback scope
Run SQLite backup after-work callbacks inside an internal callback scope. This drains promise reactions and next ticks before the event loop can become idle. Add a child-process regression test where backup is the final active request. Assisted-by: Codex Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65666 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Richard Lau <richard.lau@ibm.com>
1 parent 2203ac9 commit b7b18fd

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 6 additions & 2 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { backup, DatabaseSync } from 'node:sqlite';
2+
3+
const source = new DatabaseSync(':memory:');
4+
source.exec(`
5+
CREATE TABLE data(value);
6+
INSERT INTO data VALUES (zeroblob(1048576));
7+
`);
8+
9+
let keepAlive = setInterval(() => {}, 1_000);
10+
let settled = false;
11+
12+
process.once('beforeExit', () => {
13+
if (!settled) {
14+
process.stderr.write('backup promise did not settle before the event loop became idle\n');
15+
process.exit(1);
16+
}
17+
});
18+
19+
backup(source, process.argv[2], {
20+
rate: 1,
21+
progress() {
22+
if (keepAlive !== undefined) {
23+
clearInterval(keepAlive);
24+
keepAlive = undefined;
25+
}
26+
},
27+
}).then(() => {
28+
settled = true;
29+
source.close();
30+
}, (error) => {
31+
settled = true;
32+
process.stderr.write(`${error.stack ?? error}\n`);
33+
process.exitCode = 1;
34+
});

‎test/parallel/test-sqlite-backup.mjs‎

Lines changed: 16 additions & 1 deletion

0 commit comments

Comments
 (0)