From 095c14364d2afb8aab8445a291eb842b1ef82aa9 Mon Sep 17 00:00:00 2001 From: Isaac Cheng <47993930+IsaacCheng9@users.noreply.github.com> Date: Sun, 10 May 2026 23:56:52 +0100 Subject: [PATCH] fix: Remove misleading shared lock from Memtable iteration methods --- src/memtable.hpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/memtable.hpp b/src/memtable.hpp index dab50e2..f2882ca 100644 --- a/src/memtable.hpp +++ b/src/memtable.hpp @@ -17,14 +17,15 @@ class Memtable { void remove(const std::string &key); void clear(); - [[nodiscard]] auto begin() const { - std::shared_lock lock(mutex_); - return data_.begin(); - } - [[nodiscard]] auto end() const { - std::shared_lock lock(mutex_); - return data_.end(); - } + // Iteration is NOT internally synchronised. The previous version took a + // shared_lock here but released it at function return, leaving the + // returned iterator unprotected against concurrent writers - the lock + // was guarding nothing. Callers must externally exclude writers for the + // iteration's lifetime (e.g. Engine::flush_if_full holds write_mutex_ + // while iterating to write an SSTable). For callers without an + // exclusion guarantee, use snapshot() instead. + [[nodiscard]] auto begin() const { return data_.begin(); } + [[nodiscard]] auto end() const { return data_.end(); } [[nodiscard]] std::size_t size() const { std::shared_lock lock(mutex_); return data_.size();