]> git.hungrycats.org Git - bees/commitdiff
extent-tree: try_lock in BtrfsExtentNode::print to avoid a status deadlock
authorZygo Blaxell <bees@furryterror.org>
Fri, 10 Jul 2026 03:44:45 +0000 (23:44 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:03 +0000 (15:00 -0400)
BeesBtrfsExtentNode::print() takes the node's m_lazy_mutex to snapshot the lazy
compress/generation/datacow fields ("extent-tree: lock m_lazy_mutex in
BtrfsExtentNode print/dump_tree").  But print() runs from the
per-thread status callbacks invoked by BeesNote::get_status(), which hold
BeesNote::s_mutex the whole time, while a reference-fetching worker holds the
same node's m_lazy_mutex and then takes s_mutex via the BEESNOTE inside
refs_fetch — the opposite lock order.  The reporter and the fetcher deadlock:
the reporter holds s_mutex waiting for the node lock, the fetcher holds the node
lock waiting for s_mutex.  Observed as a hung daemon with the reporter in
get_status -> operator<<(BeesExtent) -> print and a worker in
node::refs -> refs_fetch -> BEESNOTE.

A status printer must never block on a data lock.  Acquire m_lazy_mutex with
try_to_lock and, when it is held by a concurrent fetch, leave the lazy fields
unknown (the stream formatting already handles the empty optionals).  A
best-effort status line is worth more than a hang.  The lazy-field snapshot is
still race-free — it only reads when the lock was actually acquired.

Assisted-by: Claude-Code:claude-opus-4-8
src/bees-extent-tree.cc

index f7e07014aae05ef5b6f77ee85e554f63983af28a..08d6131c06da7a468b0d8f6cd7dffa0338e77560 100644 (file)
@@ -2638,16 +2638,24 @@ BeesBtrfsExtentNode::print(ostream &os) const
        // while holding the lock — readers must do the same to avoid a
        // torn optional<> read.  m_bytenr/m_phys_size are immutable
        // post-construction; m_refs uses atomic_load by contract.
-       // Snapshot then release before streaming so a slow ostream sink
-       // can't block concurrent fetches on the same node.
+       //
+       // try_lock, never block: print() runs from the status callbacks in
+       // BeesNote::get_status(), which hold BeesNote::s_mutex, while a fetching
+       // thread holds this node's m_lazy_mutex and then takes s_mutex via the
+       // BEESNOTE inside refs_fetch — the opposite order.  Blocking here on a node
+       // a fetcher holds would deadlock the reporter against that fetcher.  If the
+       // lock is held, leave the lazy fields unknown; a best-effort status line is
+       // worth more than a hang.
        optional<int>      ct;
        optional<uint64_t> gen;
        optional<bool>     dc;
        {
-               unique_lock<recursive_mutex> lock(m_lazy_mutex);
-               ct  = m_compress_type;
-               gen = m_generation;
-               dc  = m_datacow;
+               unique_lock<recursive_mutex> lock(m_lazy_mutex, try_to_lock);
+               if (lock.owns_lock()) {
+                       ct  = m_compress_type;
+                       gen = m_generation;
+                       dc  = m_datacow;
+               }
        }
        os << "BtrfsExtent { bytenr " << to_hex(m_bytenr)
           << ", size " << pretty(m_phys_size)