tursodatabase / libsql

libSQL is a fork of SQLite that is both Open Source, and Open Contributions.
https://turso.tech/libsql
MIT License
9.54k stars 252 forks source link

fix bottomless deadlock #1494

Closed MarinPostma closed 3 months ago

MarinPostma commented 3 months ago

When locking the replicator in the wal, bottomless assumed that it could hold on the lock across await points because the lock was uncontended. This assumption was not true, so when another thread tried to acquire the lock from a runtime thread, there was a slim chance that the reactor was on that thread, and the runtime would get blocked. the checkpointing thread could not make any progress because the runtime was blocked, and the runtime thread could not acquire the lock, because the checkpointing thread could not make progress. IOW, a deadlock.

This PR replaces the replicator lock from a std for an async lock. This is, of course, a poor man's fix, but it's not worth making any better with bottomless2 being cooked.

/!\: with an async replicator lock, be careful to always call sqlite in a spawn_blocking content, or sqld will panic.