Commit 1ae37202 authored by Tobias Koppers's avatar Tobias Koppers

Fix flush() safety docs and make parallel flush sequential to prevent UB

parent fc5689a8
...@@ -238,8 +238,9 @@ impl<K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize> ...@@ -238,8 +238,9 @@ impl<K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize>
/// ///
/// # Safety /// # Safety
/// ///
/// Caller must ensure that no concurrent put or delete operation is happening on the flushed /// Caller must ensure that no concurrent `put` or `delete` operation is happening on *any*
/// family. /// family (not just the flushed one), because the implementation accesses thread-local state
/// that spans all families. Concurrent flushes of different families are also not allowed.
#[tracing::instrument(level = "trace", skip(self))] #[tracing::instrument(level = "trace", skip(self))]
pub unsafe fn flush(&self, family: u32) -> Result<()> { pub unsafe fn flush(&self, family: u32) -> Result<()> {
// Flush the thread local collectors to the global collector. // Flush the thread local collectors to the global collector.
......
...@@ -75,8 +75,11 @@ pub trait ConcurrentWriteBatch<'a>: BaseWriteBatch<'a> + Sync + Send { ...@@ -75,8 +75,11 @@ pub trait ConcurrentWriteBatch<'a>: BaseWriteBatch<'a> + Sync + Send {
/// Flushes a key space of the write batch, reducing the amount of buffered memory used. /// Flushes a key space of the write batch, reducing the amount of buffered memory used.
/// Does not commit any data persistently. /// Does not commit any data persistently.
/// ///
/// Safety: Caller must ensure that no concurrent put or delete operation is happening on the /// # Safety
/// flushed key space. ///
/// Caller must ensure that no concurrent `put` or `delete` operation is happening on *any*
/// key space (not just the flushed one), because implementations may access shared state
/// spanning all key spaces. Concurrent flushes of different key spaces are also not allowed.
unsafe fn flush(&self, key_space: KeySpace) -> Result<()>; unsafe fn flush(&self, key_space: KeySpace) -> Result<()>;
} }
......
...@@ -283,16 +283,14 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed ...@@ -283,16 +283,14 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
{ {
let _span = tracing::trace_span!("update task data").entered(); let _span = tracing::trace_span!("update task data").entered();
process_task_data(snapshots, Some(batch))?; process_task_data(snapshots, Some(batch))?;
let span = tracing::trace_span!("flush task data").entered(); let _span = tracing::trace_span!("flush task data").entered();
parallel::try_for_each( // Flush sequentially: flush() accesses per-thread state covering all
&[KeySpace::TaskMeta, KeySpace::TaskData], // families, so concurrent flushes of different key spaces would be UB.
|&key_space| { for &key_space in &[KeySpace::TaskMeta, KeySpace::TaskData] {
let _span = span.clone().entered(); // Safety: process_task_data has completed; no concurrent put/delete
// Safety: We already finished all processing of the task data and task // on any key space.
// meta unsafe { batch.flush(key_space) }?;
unsafe { batch.flush(key_space) } }
},
)?;
} }
let mut next_task_id = get_next_free_task_id::< let mut next_task_id = get_next_free_task_id::<
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment