Skip to content

Close the writer a previous JS runtime left open before opening - #3

Merged
sleeper-luke merged 2 commits into
mainfrom
claude/reclaim-stale-writer-on-reload
Oct 28, 2026
Merged

sleeper-luke merged 2 commits into
mainfrom
claude/reclaim-stale-writer-on-reload

Conversation

@claude

@claude claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Requested via Slack thread

Before: When the JS runtime reloads inside the same process, react-native-nitro-sqlite's native connection registry keeps the previous runtime's writer open. Opening the same database again registers a second handle under that name without closing the first, so a transaction the old runtime had in flight can stay open and hold the write lock, and the new runtime's writes fail with database is locked.

After: Opening a database first closes any writer a previous runtime left registered under that name. A reopen within one runtime is unchanged.

How: A new reclaimStaleWriter(name) calls NitroSQLite.native.close(name) inside a try/catch, the same way openReader already reclaims a stale reader. openNitroConnection calls it after closeNitroConnection and before open, and only when this runtime's own map does not already hold the name. When nothing is registered, the close throws and the error is swallowed.

Limits: Statements already in flight on the old handle can still resolve to the new one; fully fixing that needs a change in native nitro-sqlite.

Tests: Three new cases in src/nitro/nitro_connection.test.ts; it and src/tests/define_sqlite_store.test.ts pass in both the dev and prod jest configs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01J5R4szy5V6TVSutrikAztt

@claude
claude Bot requested a review from sleeper-luke October 28, 2026 20:02
…before opening its own

When the JS runtime reloads inside the same process, nitro-sqlite's native registry keeps
the previous runtime's writer. nitro's open() registers a new handle under the same name
without closing the old one, so a transaction the old runtime had in flight can stay open
on the leaked handle and hold the write lock, and the new runtime's writes fail as
"database is locked".

openNitroConnection now closes a writer registered under the name by a previous runtime,
the way openReader already reclaims the reader. When nothing is registered the close
throws, which is swallowed. A reopen within one runtime is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J5R4szy5V6TVSutrikAztt
@claude
claude Bot force-pushed the claude/reclaim-stale-writer-on-reload branch from c9ef77a to f12b42b Compare October 28, 2026 20:21
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J5R4szy5V6TVSutrikAztt
@sleeper-luke
sleeper-luke marked this pull request as ready for review October 28, 2026 20:44
@sleeper-luke
sleeper-luke merged commit 87e6111 into main Oct 28, 2026
2 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.

2 participants