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>
///
/// # Safety
///
/// Caller must ensure that no concurrent put or delete operation is happening on the flushed
/// family.
/// Caller must ensure that no concurrent `put` or `delete` operation is happening on *any*
/// 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))]
pub unsafe fn flush(&self, family: u32) -> Result<()> {
// Flush the thread local collectors to the global collector.
......
......@@ -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.
/// Does not commit any data persistently.
///
/// Safety: Caller must ensure that no concurrent put or delete operation is happening on the
/// flushed key space.
/// # Safety
///
/// 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<()>;
}
......
......@@ -283,16 +283,14 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
{
let _span = tracing::trace_span!("update task data").entered();
process_task_data(snapshots, Some(batch))?;
let span = tracing::trace_span!("flush task data").entered();
parallel::try_for_each(
&[KeySpace::TaskMeta, KeySpace::TaskData],
|&key_space| {
let _span = span.clone().entered();
// Safety: We already finished all processing of the task data and task
// meta
unsafe { batch.flush(key_space) }
},
)?;
let _span = tracing::trace_span!("flush task data").entered();
// Flush sequentially: flush() accesses per-thread state covering all
// families, so concurrent flushes of different key spaces would be UB.
for &key_space in &[KeySpace::TaskMeta, KeySpace::TaskData] {
// Safety: process_task_data has completed; no concurrent put/delete
// on any key space.
unsafe { batch.flush(key_space) }?;
}
}
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