Skip to content

kysely-wasqlite-worker queryData appears to leak prepared statements #25

Description

@kolaworld

Issue

kysely-wasqlite-worker leaves a prepared statement open in the normal queryData path. After running ordinary Kysely queries through the worker, closing the database can fail with:

unable to close due to unfinalized statements or unfinished backups

The current source has the suspected manual iterator usage in packages/dialect-wasqlite-worker/src/worker/utils.ts.

The same file's streaming path uses for await, which should invoke iterator cleanup automatically: utils.ts#L70-L106.

Suspected cause

In queryData, the code manually creates an async iterator from core.sqlite.statements(core.pointer, sql), calls next(), and keeps the yielded statement. It does not call return() on that iterator after the query finishes.

@subframe7536/sqlite-wasm documents that statements(...) owns the prepared statement lifetime through the iterator. It also explicitly says that manual iterator users, such as code calling next() directly, should call return() when iteration is abandoned so allocated resources are released: sqlite-wasm/src/types/api.ts#L529-L566.

That matches the observed close failure. SQLite itself reports a close error when there are unfinalized prepared statements: sqlite3_close and sqlite3_prepare_v3.

Minimal Reproduction

See repo with svelte kit minimal reproduction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions