Install pre-close vtab on readonly connections - #207
Conversation
rkistner
left a comment
There was a problem hiding this comment.
The approach sounds good to me. Just want to check: How often would ensure_has_internal_close_vtab be called now? Just to confirm we're not effectively just preparing SELECT 1 FROM powersync_internal_close on every call now, which would kinda defeat the purpose of using prepared statements. It looks like it's called at most once per connection, but just want to confirm.
It's just done once per connection, I also confirmed this with breakpoints to be sure. The only call site that creates a new storage adapter is |
Some functions using
StorageAdapterstate may reasonably be called on read-only connections:powersync_offline_sync_status()is read-only, and uses the adapter since Use microsecond precision for timestamps #183.powersync_control('target_checkpoint_request_id', null)(added in 0.5.1) is read-only, and also uses the adapter.Using the storage adapter is generally a good thing because it caches prepared statements. However, these statements need to be finalized before the database can fully close. For write connections, SDKs call
powersync_init()which registers a custom vtab for this purpose. This function is not called on read-only connections though, so these statements leak and the connection is not properly closed.As a simple fix that won't require SDK changes, this installs the pre-close hook only when it's needed to clean something up: When hooks are installed or when the storage adapter is created. For this to work on read-only connections, the vtab is now based on
SELECTstatements asINSERTs, even into virtual tables, are forbidden.AI use: Migrating the vtab was done by Claude Code.