Repository navigation
fix(backup): close secondary database handle after backup/restore - #135
Merged
andreaTP merged 2 commits intoNov 13, 2026
Merged
Conversation
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.
Collaborator
|
Released in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
WasmDB.backup()opens the destination database in the in-memory VFS withsqlite3_open_v2and never closes it.sqlite3_backup_finish()does not close the destination handle (per SQLite semantics — it only frees the backup object), so:Files.deleteIfExists(dest)after copying the file to the host marks it deleted, but content reclamation is deferred while the handle is open — forever, since nothing ever closes itbackup()call therefore retains a full database image in heap memory (its blocks never return to the free pool), until the whole file system is closedFor a workload backing up a ~20 MB database every 10 s this exhausts a 512 MB heap in about 3 minutes — one leaked image per backup call.
restore()has the same gap for the source handle, and additionally freesdestNamePtrtwice while never freeingsourceNamePtr.Fix
backup(): afterbackupFinish+extendedErrorcode, close the destination handle. The close return code is propagated when everything before it wasSQLITE_OK.restore(): same for the source handle, plus the wrong-pointer free fix.Testing
BackupTest: 4/4 pass on top of currentmain.backup()calls on one ~20 MB database, measuring heap delta per call):OutOfMemoryErroraround call 21