Skip to content

fix(backup): close secondary database handle after backup/restore - #135

Merged
andreaTP merged 2 commits into
roastedroot:mainfrom
bigmonkey567:fix-backup-dest-handle-leak
Nov 13, 2026
Merged

andreaTP merged 2 commits into
roastedroot:mainfrom
bigmonkey567:fix-backup-dest-handle-leak

Conversation

@bigmonkey567

Copy link
Copy Markdown

Problem

WasmDB.backup() opens the destination database in the in-memory VFS with sqlite3_open_v2 and never closes it. sqlite3_backup_finish() does not close the destination handle (per SQLite semantics — it only frees the backup object), so:

  • the WASI fd stays open, and the VFS file's open count never drops to 0
  • the 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 it
  • every backup() call therefore retains a full database image in heap memory (its blocks never return to the free pool), until the whole file system is closed

For 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 frees destNamePtr twice while never freeing sourceNamePtr.

Fix

  • backup(): after backupFinish + extendedErrorcode, close the destination handle. The close return code is propagated when everything before it was SQLITE_OK.
  • restore(): same for the source handle, plus the wrong-pointer free fix.

Testing

  • BackupTest: 4/4 pass on top of current main.
  • Heap probe (sequential backup() calls on one ~20 MB database, measuring heap delta per call):
    • before: monotonic +20–27 MB per call → OutOfMemoryError around call 21
    • after: flat across 25 consecutive backups (0 KB delta per call)

bigmonkey567 and others added 2 commits November 13, 2026 10:30
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.

@andreaTP andreaTP left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@andreaTP
andreaTP merged commit c39705d into roastedroot:main Nov 13, 2026
13 checks passed
@andreaTP

Copy link
Copy Markdown
Collaborator

Released in 3.54.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants