Skip to content

Fix recurring crash when Connection deinitializes - #1373

Merged
jberkel merged 1 commit into
stephencelis:masterfrom
vjymisal0:codex/close-v2-deinit-fix
Sep 13, 2026
Merged

jberkel merged 1 commit into
stephencelis:masterfrom
vjymisal0:codex/close-v2-deinit-fix

Conversation

@vjymisal0

@vjymisal0 vjymisal0 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Fixes #1276.

In Connection.deinit, calling ry? close() invokes sqlite3_close(handle). If any prepared statements, blobs, or backup operations remain unfinalized when the Connection goes out of scope, sqlite3_close() returns SQLITE_BUSY. Because the call is wrapped in ry?, the failure is swallowed, leaving the connection open and in an unmanaged state, which can trigger crashes or memory corruption when lingering statements are subsequently deallocated.

Solution

Use sqlite3_close_v2(handle) directly in Connection.deinit:

  1. SQLite's sqlite3_close_v2 is explicitly designed for reference-counted / GC environments. If open statements remain, it puts the connection into a 'zombie' state and automatically destroys the database connection when the last statement finalizes.
  2. It bypasses queue.sync during deinit, avoiding deadlock or re-entrancy issues when deallocation occurs on arbitrary dispatch queues.

Testing

Existing unit and integration test suites cover connection lifecycle. Swift tests verified against operator and connection changes.

Comment thread Sources/SQLite/Core/Connection.swift

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.

no longer needed

Comment thread Sources/SQLite/Typed/Operators.swift Outdated
Comment thread Tests/SQLiteTests/Schema/SchemaTests.swift Outdated
Comment thread Tests/SQLiteTests/Typed/OperatorsTests.swift Outdated
@vjymisal0
vjymisal0 force-pushed the codex/close-v2-deinit-fix branch from 828cdec to 585a7f4 Compare September 13, 2026 12:36
@jberkel
jberkel merged commit 8ede695 into stephencelis:master Sep 13, 2026
3 checks 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.

Recurring crash when connection object goes out of scope

2 participants