]> git.hungrycats.org Git - bees/commitdiff
hash: close lost-update window in flush_dirty_extent
authorZygo Blaxell <bees@furryterror.org>
Sat, 5 Sep 2026 04:02:11 +0000 (00:02 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:06 +0000 (15:00 -0400)
flush_dirty_extent() copied the extent under its lock, released the
lock for the pwrite, then re-locked and cleared the dirty flag.  A
set_extent_dirty_locked() that landed during the write window was
therefore erased: the on-disk copy predates the modification, and with
the flag cleared neither the writeback loop nor the shutdown flush
revisits the extent until something else dirties it.

This is benign in practice.  The hash table is a lossy best-effort
cache, every hit is content-verified before dedupe, and the flush at
exit already accepts losing updates from Tasks still running during the
final pass.  It is also easy to close.

Clear the dirty flag before taking the snapshot, while the extent lock
is still held.  A modification during the write window sets the flag
again, so the extent is written on a later pass; the worst case is an
occasional duplicate extent write.

Do not restore the flag if the write fails.  Re-marking the extent dirty
would retry the same write on every flush pass (a pass runs whenever any
hash update signals the writeback thread), so a persistently failing
write (ENOSPC on the hash table filesystem, for example) becomes a burst
of failing pwrites and logged exceptions per pass.  Leave the extent
clean instead: the on-disk copy stays stale until the next update to
that extent dirties it again, the same best-effort loss accepted at
shutdown, and each failure is logged once per dirtying.

Rate limit failed attempts as well.  flush_dirty_extents() only slept
after a successful write, so a slow pass over a table whose writes all
fail degenerated into back-to-back failures.  Charge a failed attempt
the same 128 KiB slot as a successful one so the failure rate is bounded
by the same writeback rate limit.

Assisted-by: Claude-Code:claude-fable-5-1
src/bees-hash.cc

index d1ad458029616f5e83a7cf56836c9e2dedb24159..2822b9e1b50e59c892692d44d8e9c62580076fce 100644 (file)
@@ -187,6 +187,16 @@ BeesHashTable::flush_dirty_extent(uint64_t extent_index)
                // Copy the extent because we might be stuck writing for a while
                ByteVector extent_copy(dirty_extent, dirty_extent_end);
 
+               // Mark the extent clean before releasing the lock:  a modification
+               // during the write window sets the flag again, so the extent is
+               // flushed on a later pass instead of being marked clean while the
+               // on-disk copy predates the modification.  The flag is not
+               // restored if the write fails:  the on-disk copy stays stale
+               // until the next update dirties the extent, which is the same
+               // best-effort loss accepted at shutdown, and avoids retrying a
+               // persistently failing write (e.g. ENOSPC) on every pass.
+               m_extent_metadata.at(extent_index).m_dirty = false;
+
                // Release the lock
                lock.unlock();
 
@@ -201,10 +211,6 @@ BeesHashTable::flush_dirty_extent(uint64_t extent_index)
                        bees_fsync(m_fd);
                }
 
-               // Mark extent clean if write was successful
-               lock.lock();
-               m_extent_metadata.at(extent_index).m_dirty = false;
-
                wrote_extent = true;
        });
 
@@ -229,16 +235,19 @@ BeesHashTable::flush_dirty_extents(bool slowly)
                if (flush_dirty_extent(extent_index)) {
                        ++wrote_extents;
                        ++m_writeback_extent_count;
-                       if (slowly) {
-                               if (m_stop_requested) {
-                                       slowly = false;
-                                       continue;
-                               }
-                               BEESNOTE("flush rate limited after extent #" << extent_index << " of " << m_extents << " extents");
-                               chrono::duration<double> sleep_time(m_flush_rate_limit.sleep_time(BLOCK_SIZE_HASHTAB_EXTENT));
-                               unique_lock<mutex> lock(m_stop_mutex);
-                               m_stop_condvar.wait_for(lock, sleep_time);
+               }
+               // Rate limit the attempt whether or not it succeeded, so a
+               // persistently failing write does not turn a pass into a
+               // burst of failures and logged exceptions.
+               if (slowly) {
+                       if (m_stop_requested) {
+                               slowly = false;
+                               continue;
                        }
+                       BEESNOTE("flush rate limited after extent #" << extent_index << " of " << m_extents << " extents");
+                       chrono::duration<double> sleep_time(m_flush_rate_limit.sleep_time(BLOCK_SIZE_HASHTAB_EXTENT));
+                       unique_lock<mutex> lock(m_stop_mutex);
+                       m_stop_condvar.wait_for(lock, sleep_time);
                }
        }
        BEESLOGINFO("Flushed " << wrote_extents << " of " << m_extents << " hash table extents");