Skip to content

Sqlite.close: no second sqlite3_close on freed memory - #61

Merged
junjihashimoto merged 1 commit into
mainfrom
sqlite-close
Nov 29, 2026
Merged

junjihashimoto merged 1 commit into
mainfrom
sqlite-close

Conversation

@junjihashimoto

Copy link
Copy Markdown
Contributor

Sqlite.close called sqlite3_close(db) and left the pointer in the external object. The finalizer then closed it a second time, and any call after close used the freed connection. The comment in close said this was safe because sqlite3_close is idempotent. It is not.

Change

  • The external object holds a box { sqlite3 *db; }. close closes the connection and sets db to NULL, so every reference to the Db sees it closed:
    • exec, execp and query fail with …: database is closed;
    • a second close does nothing;
    • the finalizer closes the connection only if it is still open, then frees the box.
  • lean_set_external_data can't do this: on a shared object it returns a new object, and the other references keep the old pointer. The new comment says so.
  • Prepared statements are prepared and finalized within one call and never reach Lean, so none can outlive the connection.
  • The docstrings of Db and close describe the new behaviour.

Tests

persist_spec has a new Sqlite.close group (8 assertions):

  • close twice;
  • exec / execp / query after close;
  • the same through a Backend that holds another reference;
  • 100 connections left to the finalizer, closed or not;
  • rows written before close are still there.
lake build persist_spec
./.lake/build/bin/persist_spec                                   # 46 passed, 0 failed
valgrind --error-exitcode=9 ./.lake/build/bin/persist_spec       # ERROR SUMMARY: 0 errors

With the old leantea_sqlite.c and the new spec, valgrind reports invalid reads in sqlite3_close (second close) and sqlite3_prepare_v2 (use after close) on blocks freed by the first sqlite3_close.

I didn't try ASan: the static library is linked by Lean's bundled toolchain, which has no sanitizer runtime.

Not covered

A close racing with a call on another thread is still a data race on the box. Before this change that case was a use-after-free.

`close` called `sqlite3_close(db)` and left the pointer in the external
object, so the finalizer closed it again later, and any call after
`close` used the freed connection. The comment said this was fine
because sqlite3_close is idempotent; it is not.

The external object now holds a small box `{ sqlite3 *db; }`. `close`
closes the connection and sets `db` to NULL in the box, so every
reference to the `Db` sees it closed: `exec`, `execp` and `query` fail
with "database is closed", a second `close` does nothing, and the
finalizer closes the connection only if it is still open, then frees
the box. (`lean_set_external_data` could not do this: on a shared
object it returns a new one and the other references keep the old
pointer.) Statements are prepared and finalized within one call, so
none outlives the connection.

persist_spec has a `Sqlite.close` group: close twice, exec / execp /
query after close, the same through a `Backend` holding another
reference, and 100 connections left to the finalizer, closed or not.
Under valgrind the old code reports invalid reads in sqlite3_close and
sqlite3_prepare_v2; with this change it reports 0 errors.
@junjihashimoto
junjihashimoto merged commit 7fcbb7f into main Nov 29, 2026
1 check passed
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.

1 participant