mozilla / rkv

A simple, humane, typed key-value storage solution.
https://crates.io/crates/rkv
Apache License 2.0
304 stars 52 forks source link

Make sure there is no parallel file write by `write_to_disk` #233

Open saschanaz opened 1 year ago

saschanaz commented 1 year ago

More details here: https://bugzilla.mozilla.org/show_bug.cgi?id=1807010#c2

rkv's safe mode protects the in-memory database contents with an RwLock (https://github.com/mozilla/rkv/blob/c5a7594c830903f59eb28c7e32daa8200744323e/src/backend/impl_safe/environment.rs#L234-L236), but that lock is released in https://github.com/mozilla/rkv/blob/c5a7594c830903f59eb28c7e32daa8200744323e/src/backend/impl_safe/transaction.rs#L192, before flushing the contents to disk. So, even if (2) weren't a factor, multiple calls to EnvironmentImpl.write_to_disk() from different task queues could still race with each other.

I wonder we can mitigate this issue by adding a separate lock solely for this write_to_disk() and wait for the lock for each write.