The v1->v2 data migration ran fsync/rename/remove on libSQL SQLite files whose native OS handle lingers past close() on Windows, crashing the daemon at boot with a fatal "Unknown error". POSIX never hits this, so it only reproduced on Windows (the v1.5.23 desktop/CLI startup regression). - fsyncFileIfExists opens read-write, since Windows FlushFileBuffers needs a writable handle (a read-only fd throws EPERM), and treats fsync as best-effort durability hardening. - remove of the source/staging file sets now retries EBUSY/EPERM the same way rename already did. - rename/remove retries force a GC pass first so libSQL's native finalizer releases the handle before the next attempt. Verified on a Windows VM: a seeded v1 database now migrates to v2 and the daemon announces ready.
Showing the changes to apps (1 of 2 files). Show all
Unified · Split
| @@ -1003,10 +1003,19 @@ const insertPlan = async ( · 1002 unmodified lines | ||
| 1003 | 1003 | } |
| 1004 | 1004 | }; |
| 1005 | 1005 | |
| 1006 | +// libSQL releases its OS file handle in a native finalizer, which on Windows | |
| 1007 | +// only runs on garbage collection and so lags client.close(). Forcing a GC | |
| 1008 | +// runs the finalizer, releasing the handle so a follow-up rename/rm succeeds. | |
| 1009 | +// No-op outside the Bun runtime (e.g. vitest on Node), where this is unneeded. | |
| 1010 | +const nudgeNativeHandleRelease = (): void => { | |
| 1011 | + const bun = (globalThis as { Bun?: { gc?: (sync: boolean) => void } }).Bun; | |
| 1012 | + bun?.gc?.(true); | |
| 1013 | +}; | |
| 1014 | + | |
| 1006 | 1015 | // Windows reports EBUSY/EPERM on rename for a short window after a SQLite |
| 1007 | 1016 | // handle closes (handle release lags the close call, and antivirus scanners |
| 1008 | 1017 | // briefly lock the file). POSIX renames never hit this — retry with a short |
| 1009 | −// backoff instead of failing the whole migration. | |
| 1018 | +// backoff, forcing handle release first, instead of failing the whole migration. | |
| 1010 | 1019 | const renameWithRetry = async (source: string, target: string): Promise<void> => { |
| 1011 | 1020 | // ~8s total. 1.9s was not enough on Windows: libSQL's native handle and |
| 1012 | 1021 | // antivirus scans hold the freshly-written db past close() (observed as an |
| @@ -1019,16 +1028,51 @@ const renameWithRetry = async (source: string, target: string): Promise<void> => · 6 unmodified lines | ||
| 1019 | 1028 | } catch (cause) { |
| 1020 | 1029 | const code = (cause as NodeJS.ErrnoException).code; |
| 1021 | 1030 | if ((code !== "EBUSY" && code !== "EPERM") || attempt >= delaysMs.length) throw cause; |
| 1031 | + nudgeNativeHandleRelease(); | |
| 1032 | + await new Promise((resolveDelay) => setTimeout(resolveDelay, delaysMs[attempt])); | |
| 1033 | + } | |
| 1034 | + } | |
| 1035 | +}; | |
| 1036 | + | |
| 1037 | +// Same lingering-handle window as renameWithRetry: on Windows a libSQL file's | |
| 1038 | +// native handle (and antivirus locks) can briefly outlive close(), so removing | |
| 1039 | +// a just-closed source/staging db throws EBUSY/EPERM. POSIX never hits this. | |
| 1040 | +const rmWithRetry = async (path: string): Promise<void> => { | |
| 1041 | + const delaysMs = [50, 100, 250, 500, 1000, 2000, 4000]; | |
| 1042 | + for (let attempt = 0; ; attempt++) { | |
| 1043 | + try { | |
| 1044 | + fs.rmSync(path, { force: true }); | |
| 1045 | + return; | |
| 1046 | + } catch (cause) { | |
| 1047 | + const code = (cause as NodeJS.ErrnoException).code; | |
| 1048 | + if ((code !== "EBUSY" && code !== "EPERM") || attempt >= delaysMs.length) throw cause; | |
| 1049 | + nudgeNativeHandleRelease(); | |
| 1022 | 1050 | await new Promise((resolveDelay) => setTimeout(resolveDelay, delaysMs[attempt])); |
| 1023 | 1051 | } |
| 1024 | 1052 | } |
| 1025 | 1053 | }; |
| 1026 | 1054 | |
| 1055 | +const removeSqliteFileSetWithRetry = async (path: string): Promise<void> => { | |
| 1056 | + for (const suffix of fileSetSuffixes) await rmWithRetry(`${path}${suffix}`); | |
| 1057 | +}; | |
| 1058 | + | |
| 1027 | 1059 | const fsyncFileIfExists = (path: string): void => { |
| 1028 | 1060 | if (!fs.existsSync(path)) return; |
| 1029 | − const fd = fs.openSync(path, "r"); | |
| 1061 | + // Windows FlushFileBuffers requires a writable handle, so fsync on a | |
| 1062 | + // read-only ("r") fd throws EPERM and would crash the v1->v2 migration at | |
| 1063 | + // boot. Open read-write for the flush, and treat fsync as best-effort | |
| 1064 | + // durability hardening (the rename/copy already wrote the bytes) so a | |
| 1065 | + // platform or filesystem that refuses the flush does not fail the migration. | |
| 1066 | + let fd: number; | |
| 1067 | + try { | |
| 1068 | + fd = fs.openSync(path, "r+"); | |
| 1069 | + } catch { | |
| 1070 | + return; | |
| 1071 | + } | |
| 1030 | 1072 | try { |
| 1031 | 1073 | fs.fsyncSync(fd); |
| 1074 | + } catch { | |
| 1075 | + // best-effort: some platforms/filesystems refuse fsync on certain handles | |
| 1032 | 1076 | } finally { |
| 1033 | 1077 | fs.closeSync(fd); |
| 1034 | 1078 | } |
| @@ -1060,7 +1104,7 @@ const moveSqliteFileSet = async (source: string, target: string): Promise<void> · 25 unmodified lines | ||
| 1060 | 1104 | await renameWithRetry(`${source}${suffix}`, `${target}${suffix}`); |
| 1061 | 1105 | fsyncDirectory(dirname(target)); |
| 1062 | 1106 | } else { |
| 1063 | − fs.rmSync(`${target}${suffix}`, { force: true }); | |
| 1107 | + await rmWithRetry(`${target}${suffix}`); | |
| 1064 | 1108 | } |
| 1065 | 1109 | } |
| 1066 | 1110 | }; |
| @@ -1071,7 +1115,7 @@ const moveExistingSqliteFileSet = async (source: string, target: string): Promis · 4 unmodified lines | ||
| 1071 | 1115 | if (!fs.existsSync(from)) continue; |
| 1072 | 1116 | const to = `${target}${suffix}`; |
| 1073 | 1117 | if (fs.existsSync(to)) { |
| 1074 | − fs.rmSync(from, { force: true }); | |
| 1118 | + await rmWithRetry(from); | |
| 1075 | 1119 | } else { |
| 1076 | 1120 | await renameWithRetry(from, to); |
| 1077 | 1121 | fsyncDirectory(dirname(to)); |
| @@ -1413,8 +1457,8 @@ const completeJournaledFlip = async ( · 335 unmodified lines | ||
| 1413 | 1457 | await pauseMigrationForTest("staging-consumed"); |
| 1414 | 1458 | } |
| 1415 | 1459 | await writeMigrationJournal(journalPath, { ...journal, phase: "committed" }); |
| 1416 | − removeSqliteFileSet(journal.normalizedSource); | |
| 1417 | − removeSqliteFileSet(journal.staging); | |
| 1460 | + await removeSqliteFileSetWithRetry(journal.normalizedSource); | |
| 1461 | + await removeSqliteFileSetWithRetry(journal.staging); | |
| 1418 | 1462 | await cleanupSecretBackups(journal); |
| 1419 | 1463 | removeMigrationJournal(journalPath); |
| 1420 | 1464 | }; |
| @@ -1435,8 +1479,8 @@ const recoverV1V2Migration = async (sqlitePath: string): Promise<MigrationRecove · 14 unmodified lines | ||
| 1435 | 1479 | |
| 1436 | 1480 | const { journal } = journalRead; |
| 1437 | 1481 | if (journal.phase === "building") { |
| 1438 | − removeSqliteFileSet(journal.normalizedSource); | |
| 1439 | − removeSqliteFileSet(journal.staging); | |
| 1482 | + await removeSqliteFileSetWithRetry(journal.normalizedSource); | |
| 1483 | + await removeSqliteFileSetWithRetry(journal.staging); | |
| 1440 | 1484 | await restoreAuthFromJournal(journal); |
| 1441 | 1485 | await cleanupSecretBackups(journal); |
| 1442 | 1486 | removeMigrationJournal(journalPath); |
| @@ -1448,8 +1492,8 @@ const recoverV1V2Migration = async (sqlitePath: string): Promise<MigrationRecove · 5 unmodified lines | ||
| 1448 | 1492 | return "recovered"; |
| 1449 | 1493 | } |
| 1450 | 1494 | |
| 1451 | − removeSqliteFileSet(journal.normalizedSource); | |
| 1452 | − removeSqliteFileSet(journal.staging); | |
| 1495 | + await removeSqliteFileSetWithRetry(journal.normalizedSource); | |
| 1496 | + await removeSqliteFileSetWithRetry(journal.staging); | |
| 1453 | 1497 | await cleanupSecretBackups(journal); |
| 1454 | 1498 | removeMigrationJournal(journalPath); |
| 1455 | 1499 | return "recovered"; |
| @@ -1573,8 +1617,7 @@ export const migrateLocalV1ToV2IfNeeded = async ( · 117 unmodified lines | ||
| 1573 | 1617 | // WAL/SHM sidecars before journaling the staged DB as complete; otherwise a |
| 1574 | 1618 | // crash mid-sidecar move would make staging appear present after the base DB |
| 1575 | 1619 | // had already been installed. |
| 1576 | − for (const suffix of ["-wal", "-shm"] as const) | |
| 1577 | − fs.rmSync(`${stagingPath}${suffix}`, { force: true }); | |
| 1620 | + for (const suffix of ["-wal", "-shm"] as const) await rmWithRetry(`${stagingPath}${suffix}`); | |
| 1578 | 1621 | fsyncDirectory(dirname(stagingPath)); |
| 1579 | 1622 | await writeMigrationJournal(journalPath, { ...journal, phase: "built" }); |
| 1580 | 1623 | await pauseMigrationForTest("built"); |
| @@ -1598,8 +1641,8 @@ export const migrateLocalV1ToV2IfNeeded = async ( · 17 unmodified lines | ||
| 1598 | 1641 | await restoreAuthFromJournal(journal); |
| 1599 | 1642 | await cleanupSecretBackups(journal); |
| 1600 | 1643 | } |
| 1601 | − if (normalizedSourcePath) removeSqliteFileSet(normalizedSourcePath); | |
| 1602 | − if (stagingPath) removeSqliteFileSet(stagingPath); | |
| 1644 | + if (normalizedSourcePath) await removeSqliteFileSetWithRetry(normalizedSourcePath); | |
| 1645 | + if (stagingPath) await removeSqliteFileSetWithRetry(stagingPath); | |
| 1603 | 1646 | removeMigrationJournal(journalPath); |
| 1604 | 1647 | } |
| 1605 | 1648 | throw cause; |