|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +common.skipIfSQLiteMissing(); |
| 5 | + |
| 6 | +if (common.isWindows) { |
| 7 | + common.skip('SQLite on Windows uses HANDLEs, not fds'); |
| 8 | +} |
| 9 | + |
| 10 | +const tmpdir = require('../common/tmpdir'); |
| 11 | +const assert = require('node:assert'); |
| 12 | +const { spawnPromisified } = common; |
| 13 | +const { writeFileSync } = require('node:fs'); |
| 14 | +const { join } = require('node:path'); |
| 15 | +const { DatabaseSync } = require('node:sqlite'); |
| 16 | +const { test } = require('node:test'); |
| 17 | + |
| 18 | +tmpdir.refresh(); |
| 19 | + |
| 20 | +// Regression tests for https://github.com/nodejs/node/issues/64640. Failed |
| 21 | +// localStorage initialisation leaked the SQLite connection opened by |
| 22 | +// sqlite3_open(), which usually returns a database handle even on failure. |
| 23 | +// Each retry then leaked one more file descriptor. |
| 24 | +async function assertFailedInitDoesNotLeak(localStorageFile, message) { |
| 25 | + const cp = await spawnPromisified(process.execPath, [ |
| 26 | + '--localstorage-file', localStorageFile, |
| 27 | + '-e', ` |
| 28 | + const assert = require('node:assert'); |
| 29 | + const { openSync, closeSync } = require('node:fs'); |
| 30 | + // The lowest fd available to a new file. If a failed localStorage |
| 31 | + // initialisation leaks its connection, this number grows. |
| 32 | + const probeFd = () => { |
| 33 | + const fd = openSync(process.execPath, 'r'); |
| 34 | + closeSync(fd); |
| 35 | + return fd; |
| 36 | + }; |
| 37 | + const expected = { code: 'ERR_INVALID_STATE', message: ${message} }; |
| 38 | + // Warm up lazily initialised resources before sampling the fd space. |
| 39 | + assert.throws(() => localStorage.length, expected); |
| 40 | + const before = probeFd(); |
| 41 | + for (let i = 0; i < 15; i++) { |
| 42 | + assert.throws(() => localStorage.length, expected); |
| 43 | + } |
| 44 | + assert.strictEqual(probeFd(), before); |
| 45 | + `, |
| 46 | + ]); |
| 47 | + |
| 48 | + assert.strictEqual(cp.stderr, ''); |
| 49 | + assert.strictEqual(cp.code, 0); |
| 50 | + assert.strictEqual(cp.signal, null); |
| 51 | +} |
| 52 | + |
| 53 | +test('corrupt non-SQLite file does not leak fds', async () => { |
| 54 | + const file = join(tmpdir.path, 'corrupt.localstorage'); |
| 55 | + writeFileSync(file, 'not a sqlite database '.repeat(10)); |
| 56 | + await assertFailedInitDoesNotLeak(file, '/not a database/'); |
| 57 | +}); |
| 58 | + |
| 59 | +test('unopenable database path does not leak fds', async () => { |
| 60 | + const file = join(tmpdir.path, 'missing-dir', 'db.localstorage'); |
| 61 | + await assertFailedInitDoesNotLeak(file, '/unable to open database file/'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('newer schema version does not leak fds', async () => { |
| 65 | + const file = join(tmpdir.path, 'newer-schema.localstorage'); |
| 66 | + const db = new DatabaseSync(file); |
| 67 | + db.exec(`CREATE TABLE nodejs_webstorage_state( |
| 68 | + max_size INTEGER NOT NULL DEFAULT 10485760, |
| 69 | + total_size INTEGER NOT NULL, |
| 70 | + schema_version INTEGER NOT NULL DEFAULT 0, |
| 71 | + single_row_ INTEGER NOT NULL DEFAULT 1 CHECK(single_row_ = 1), |
| 72 | + PRIMARY KEY(single_row_) |
| 73 | + ) STRICT; |
| 74 | + INSERT INTO nodejs_webstorage_state (total_size, schema_version) |
| 75 | + VALUES (0, 99);`); |
| 76 | + db.close(); |
| 77 | + await assertFailedInitDoesNotLeak(file, '/newer version of Node\\.js/'); |
| 78 | +}); |
| 79 | + |
| 80 | +test('empty state table does not leak fds', async () => { |
| 81 | + const file = join(tmpdir.path, 'zero-rows.localstorage'); |
| 82 | + const db = new DatabaseSync(file); |
| 83 | + // The extra NOT NULL column makes the initialisation script's |
| 84 | + // INSERT OR IGNORE skip silently, leaving the state table empty, so |
| 85 | + // Open() fails while a prepared statement is still live. |
| 86 | + db.exec(`CREATE TABLE nodejs_webstorage_state( |
| 87 | + max_size INTEGER NOT NULL DEFAULT 10485760, |
| 88 | + total_size INTEGER NOT NULL, |
| 89 | + schema_version INTEGER NOT NULL DEFAULT 0, |
| 90 | + single_row_ INTEGER NOT NULL DEFAULT 1 CHECK(single_row_ = 1), |
| 91 | + extra INTEGER NOT NULL, |
| 92 | + PRIMARY KEY(single_row_) |
| 93 | + ) STRICT;`); |
| 94 | + db.close(); |
| 95 | + await assertFailedInitDoesNotLeak(file, '/no more rows available/'); |
| 96 | +}); |
| 97 | + |
| 98 | +test('non-integer schema version throws instead of aborting', async () => { |
| 99 | + const file = join(tmpdir.path, 'text-schema.localstorage'); |
| 100 | + const db = new DatabaseSync(file); |
| 101 | + db.exec(`CREATE TABLE nodejs_webstorage_state( |
| 102 | + max_size INTEGER NOT NULL DEFAULT 10485760, |
| 103 | + total_size INTEGER NOT NULL, |
| 104 | + schema_version TEXT NOT NULL, |
| 105 | + single_row_ INTEGER NOT NULL DEFAULT 1 CHECK(single_row_ = 1), |
| 106 | + PRIMARY KEY(single_row_) |
| 107 | + ) STRICT; |
| 108 | + INSERT INTO nodejs_webstorage_state (total_size, schema_version) |
| 109 | + VALUES (0, 'pwned');`); |
| 110 | + db.close(); |
| 111 | + await assertFailedInitDoesNotLeak( |
| 112 | + file, '/schema version is not an integer/'); |
| 113 | +}); |
0 commit comments