Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always release long-running fd on db close #519

Merged
merged 1 commit into from
Nov 14, 2023

Conversation

hifi
Copy link
Collaborator

@hifi hifi commented Nov 12, 2023

If Litestream is used as a library and it can be closed and re-opened against the same database this leaking fd can break SQLite database file locking. During normal cli operation this race cannot happen as the whole process will exit after closing databases.

Closing Litestream and opening again right after will leave the unclosed file handle out-of-scope and free to garbage collect. Go tries to be helpful and closes the file handle during GC to prevent it leaking permanently but that will cause a race with POSIX advisory locks.

This order of operations will release the real lock the embedded SQLite of Litestream is holding while leaving it clueless. That will allow the main application to take a new lock on the file and eventually both may be checkpointing the same WAL into the main database file causing permanent corruption.

https://www.sqlite.org/howtocorrupt.html#_posix_advisory_locks_canceled_by_a_separate_thread_doing_close_

Fixes #510

If Litestream is used as a library and it can be closed and
re-opened against the same database this leaking fd can break
SQLite database file locking. During normal cli operation this
race cannot happen as the whole process will exit after closing
databases.

Closing Litestream and opening again right after will leave the
unclosed file handle out-of-scope and free to garbage collect. Go
tries to be helpful and closes the file handle during GC to prevent
it leaking permanently but that will cause a race with POSIX
advisory locks.

This order of operations will release the _real_ lock the embedded
SQLite of Litestream is holding while leaving it clueless. That
will allow the main application to take a new lock on the file and
eventually both may be checkpointing the same WAL into the main
database file causing permanent corruption.

https://www.sqlite.org/howtocorrupt.html#_posix_advisory_locks_canceled_by_a_separate_thread_doing_close_

Fixes benbjohnson#510
@benbjohnson benbjohnson merged commit 85ddf32 into benbjohnson:main Nov 14, 2023
1 check failed
@gjtorikian
Copy link

@benbjohnson could a new patch release of the .deb file be made that includes this?

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.

Unclean close leaves Litestream in weird state
3 participants