Review report

bitcoin/bitcoin: PR #33922

Provider refusals

write_verifier: Provider declined the request because of a cybersecurity restriction. Experiments remain incomplete; static verification can continue.

1 major finding · 1 suggestion

Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive for their assessments.

Findings

Link

[major] · Template destruction can race with manager teardown during shutdown

If a trusted -ipcbind mining client retains templates until shutdown, asynchronous template destruction can access the manager during or after its reset, causing an assertion abort or a use-after-free race.

Location: src/node/interfaces.cpp:893 (head).

Details and suggested fix

The new destructor calls UnregisterTemplate() through *Assert(m_node.block_template_manager). During shutdown, disconnectIncoming() waits for connection erasure, but libmultiprocess queues retained implementations for destruction on its asynchronous cleanup thread. Shutdown does not wait for that cleanup before resetting block_template_manager; the IPC owner survives until after Shutdown() returns. A late destructor therefore aborts on the null manager, while a concurrent destructor can access manager state during destruction. Completing destroy() before shutdown avoids this path, but clients retaining templates until disconnect do not have that protection. Chainstate flushing happens earlier, so the established consequence is an unclean exit or undefined behavior, not demonstrated data loss. The failure depends on scheduling and has not been observed. Give templates shared ownership of a separate registry that outlives the manager, so their destructors do not access NodeContext. A null check alone would not remove the race. Add a shutdown test with a client retaining a template without calling destroy().


Optional improvements

Link

[suggestion] · Releasing retained templates repeatedly scans the entire registry

Releasing N retained templates individually performs N(N+1)/2 weak_ptr lock attempts under m_mutex, also blocking other clients' registration and accounting snapshots.

Location: src/node/block_template_manager.cpp:74 (head).

Details and suggested fix

UnregisterTemplate() runs std::erase_if over every registry entry and calls weak_ptr::lock() on each one. Both createNewBlock() and successful waitNext() calls register new templates, with no retained-template count limit. Explicit destruction or disconnect cleanup releases implementations individually, so clearing a client's retained templates incurs quadratic registry work. Key the non-owning registry by template address, for example with std::unordered_map<const CBlockTemplate*, std::weak_ptr<const CBlockTemplate>>, to allow expected constant-time removal while preserving accounting snapshots. Retain expired-entry maintenance where needed. The practical cost depends on client retention and has not been measured; the shutdown-lifetime correction may also reshape this path.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.