From f81eee77106e4dee5f1d9c20bdd80468dbe6d7e5 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 02:06:31 +0000 Subject: [PATCH] docs(database): Document borrowed-item soundness flaw on iterator streams --- src/database/map/stream.rs | 11 +++++++++++ src/database/stream.rs | 30 ++++++++++++++---------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/database/map/stream.rs b/src/database/map/stream.rs index b2da4743e..bf82f489c 100644 --- a/src/database/map/stream.rs +++ b/src/database/map/stream.rs @@ -12,6 +12,17 @@ impl super::Map { /// Iterate key-value entries in the map from the beginning. /// /// - Result is deserialized + /// + /// # Unsoundness + /// Because Rust's `Stream` trait does not support lending items tied to + /// `&mut self`, `stream::slice_longevity` unsafely extends the lifetime of + /// raw RocksDB iterator slices to `'a` (the borrow of `Arc`). As a + /// consequence, borrowed deserialized types (e.g. `&str`, `&UserId`, + /// `&[u8]`) reference the underlying RocksDB cursor buffer and are + /// invalidated on the next poll of the stream. Do not hold borrowed items + /// across polls (e.g. via `collect()`, `ready_chunks()`, `peekable()`, + /// `buffered()`, `buffer_unordered()`, `broadband`, or `wideband`) without + /// converting them to owned values first. pub fn stream<'a, K, V>( self: &'a Arc, ) -> impl Stream>> + Send diff --git a/src/database/stream.rs b/src/database/stream.rs index ddcaa32c8..1cf690000 100644 --- a/src/database/stream.rs +++ b/src/database/stream.rs @@ -133,22 +133,20 @@ fn keyval_longevity<'a, 'b: 'a>(item: KeyVal<'a>) -> KeyVal<'b> { } fn slice_longevity<'a, 'b: 'a>(item: &'a Slice) -> &'b Slice { - // SAFETY: The lifetime of the data returned by the rocksdb cursor is only - // valid between each movement of the cursor. It is hereby unsafely - // extended to match the lifetime of the cursor itself. This is due to the - // limitation of the Stream trait where the Item is incapable of conveying - // a lifetime; this is due to GAT's being unstable during its development. - // This unsafety can be removed as soon as this limitation is addressed by - // an upcoming version. + // SAFETY: the lifetime of the data returned by + // the RocksDB cursor is only valid until the next movement of the cursor, + // but is unsafely extended here to match the lifetime `'b` of the `Map` + // borrow because `futures::Stream` is not a lending stream. // - // We have done our best to mitigate the implications of this in conjunction - // with the deserialization API such that borrows being held across - // movements of the cursor do not happen accidentally. The compiler will - // still error when values herein produced try to leave a closure passed to - // a StreamExt API. But escapes can happen if you explicitly and - // intentionally attempt it, and there will be no compiler error or - // warning. This is primarily the case with calling collect() without a - // preceding map(ToOwned::to_owned). A collection of references here is - // illegal, but this will not be enforced by the compiler. + // Callers MUST NOT hold borrowed items (or borrowed deserialized views such + // as `&str`, `&UserId`, or `&[u8]`) across subsequent polls of the stream — + // in particular, never call `collect()`, `ready_chunks()`, `peekable()`, + // `buffered()`, `buffer_unordered()`, `broadband`, or `wideband` on a + // stream yielding borrowed references without first converting items to + // owned values (e.g. `map(ToOwned::to_owned)`). The compiler does NOT + // reject such escapes when items borrow from `'b`. + // + // TODO: Fix this, it is old code and language changes may have made it + // unnecessary unsafe { std::mem::transmute(item) } }