(statements: string[])
| 533 | } |
| 534 | |
| 535 | private async runRecoveryOffThread(statements: string[]): Promise<void> { |
| 536 | let WorkerCtor: typeof import('node:worker_threads').Worker; |
| 537 | try { |
| 538 | WorkerCtor = (await import('node:worker_threads')).Worker; |
| 539 | } catch { |
| 540 | // No worker threads in this runtime: recover here, as open() would have. |
| 541 | for (const ddl of statements) this.db.exec(ddl); |
| 542 | return; |
| 543 | } |
| 544 | // SQLite's default temp storage, not this connection's MEMORY: the sort |
| 545 | // behind a big index spills to disk instead of growing the heap. The |
| 546 | // worker collects its garbage before it ends, so it exits with no marking |
| 547 | // in flight (worker-teardown.ts), and nothing terminates it. |
| 548 | const workerSource = ` |
| 549 | const { workerData, parentPort } = require('node:worker_threads'); |
| 550 | let error = null; |
| 551 | try { |
| 552 | const { DatabaseSync } = require('node:sqlite'); |
| 553 | const db = new DatabaseSync(workerData.dbPath); |
| 554 | try { |
| 555 | db.exec('PRAGMA busy_timeout = 30000'); |
| 556 | for (const sql of workerData.statements) db.exec(sql); |
| 557 | } finally { |
| 558 | db.close(); |
| 559 | } |
| 560 | } catch (e) { |
| 561 | error = String((e && e.message) || e); |
| 562 | } |
| 563 | try { |
| 564 | const v8 = require('v8'); |
| 565 | v8.setFlagsFromString('--expose-gc'); |
| 566 | const gc = require('vm').runInNewContext('gc'); |
| 567 | v8.setFlagsFromString('--no-expose-gc'); |
| 568 | gc(); |
| 569 | } catch {} |
| 570 | parentPort.postMessage({ error }); |
| 571 | `; |
| 572 | await new Promise<void>((resolve, reject) => { |
| 573 | let settled = false; |
| 574 | const worker = new WorkerCtor(workerSource, { eval: true, workerData: { dbPath: this.dbPath, statements } }); |
| 575 | worker.once('message', (message: { error: string | null }) => { |
| 576 | settled = true; |
| 577 | if (message.error) reject(new Error(`Could not restore the indexes of ${this.dbPath}: ${message.error}`)); |
| 578 | else resolve(); |
| 579 | }); |
| 580 | worker.once('error', (err) => { |
| 581 | if (settled) return; |
| 582 | settled = true; |
| 583 | reject(err); |
| 584 | }); |
| 585 | worker.once('exit', (code) => { |
| 586 | if (settled) return; |
| 587 | settled = true; |
| 588 | reject(new Error(`Restoring the indexes of ${this.dbPath} stopped early (worker exit ${code}).`)); |
| 589 | }); |
| 590 | }); |
| 591 | } |
| 592 |
no test coverage detected