From eed3bc19ff6ae2c929256be0ba5bc7fd6ce4858b Mon Sep 17 00:00:00 2001 From: bigmonkey567 Date: Fri, 13 Nov 2026 10:30:24 +0800 Subject: [PATCH 1/2] fix(backup): close secondary database handle after backup/restore sqlite3_backup_finish does not close the destination (backup) or source (restore) database handle, so the handle opened with sqlite3_open_v2 leaks. With the in-memory VFS this is more than an fd leak: the open handle keeps the destination file alive, the Files.deleteIfExists after the copy to the host defers content reclamation, and every backup call retains another full database image on the heap (a ~20 MB database backed up every 10 s exhausts a 512 MB heap in ~3 minutes). Also fix restore() freeing destNamePtr twice while never freeing sourceNamePtr. --- .../io/roastedroot/sqlite4j/core/WasmDB.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java b/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java index 8f9a33168a..25b69ab03a 100644 --- a/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java +++ b/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java @@ -867,6 +867,15 @@ public int backup( lib.backupFinish(pBackup); rc = lib.extendedErrorcode(lib.ptr(destDbPtr)); + // sqlite3_backup_finish does NOT close the destination handle. Leaving it + // open keeps the destination file open in the VFS, so the + // Files.deleteIfExists below never reclaims its content blocks — every + // backup call retains a full database image in memory until the whole + // file system is closed. + int closeRc = lib.close(lib.ptr(destDbPtr)); + if (rc == SQLITE_OK) { + rc = closeRc; + } } lib.free(originNamePtr); @@ -969,10 +978,16 @@ public int restore( lib.backupFinish(pBackup); rc = lib.extendedErrorcode(lib.ptr(sourceDbPtr)); + // same as backup(): sqlite3_backup_finish does not close the source + // handle, and an open handle keeps the in-VFS source file alive + int closeRc = lib.close(lib.ptr(sourceDbPtr)); + if (rc == SQLITE_OK) { + rc = closeRc; + } } lib.free(destNamePtr); - lib.free(destNamePtr); + lib.free(sourceNamePtr); lib.free(sourceDbPtr); lib.free(mainStrPtr); From 31456e49fc1d750ef2fb89cf76683aa9bbfa179b Mon Sep 17 00:00:00 2001 From: andreatp Date: Fri, 13 Nov 2026 09:18:29 +0000 Subject: [PATCH 2/2] Trim comments --- src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java b/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java index 25b69ab03a..b5a448ebbe 100644 --- a/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java +++ b/src/main/java/io/roastedroot/sqlite4j/core/WasmDB.java @@ -867,11 +867,7 @@ public int backup( lib.backupFinish(pBackup); rc = lib.extendedErrorcode(lib.ptr(destDbPtr)); - // sqlite3_backup_finish does NOT close the destination handle. Leaving it - // open keeps the destination file open in the VFS, so the - // Files.deleteIfExists below never reclaims its content blocks — every - // backup call retains a full database image in memory until the whole - // file system is closed. + // backup_finish doesn't close the handle; left open, the VFS file is never reclaimed int closeRc = lib.close(lib.ptr(destDbPtr)); if (rc == SQLITE_OK) { rc = closeRc; @@ -978,8 +974,7 @@ public int restore( lib.backupFinish(pBackup); rc = lib.extendedErrorcode(lib.ptr(sourceDbPtr)); - // same as backup(): sqlite3_backup_finish does not close the source - // handle, and an open handle keeps the in-VFS source file alive + // backup_finish doesn't close the source handle int closeRc = lib.close(lib.ptr(sourceDbPtr)); if (rc == SQLITE_OK) { rc = closeRc;