Review report

bitcoin/bitcoin: PR #33922

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 cleanup can race with manager destruction during shutdown

With an authorized IPC client retaining templates during shutdown, asynchronous template cleanup can race with manager teardown and cause an unclean exit.

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

Details and suggested fix

The new ~BlockTemplateImpl dereferences m_node.block_template_manager through Assert before removing its transaction references. For a connected -ipcbind client that has not called destroy(), shutdown's disconnectIncoming() only synchronizes connection removal. ~ProxyServerBase queues implementation destruction on libmultiprocess's asynchronous cleanup thread, and shutdown proceeds to node.block_template_manager.reset() without waiting for that cleanup. If template destruction follows the reset, Assert fails; if it overlaps the reset, it races on the manager pointer and can access the manager's mutex or map during destruction. This can produce an unclean exit or core dump, although chainstate flushing and any configured mempool dump precede this point. Move the accounting map and mutex into a shared tracker retained by each template, so the destructor no longer depends on the node-owned manager's lifetime. A null check alone would not eliminate the race. The failure is scheduling-dependent and has not been observed.


Optional improvements

Link

[suggestion] · The memory estimate omits the transaction object allocation

getMemoryLoad() omits the transaction object and shared ownership allocation for each counted transaction, making the underestimate proportionally larger for small transactions.

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

Details and suggested fix

RecursiveDynamicUsage(*tx_ref) selects the CTransaction overload, which counts the input/output vectors and nested scripts and witnesses but not the allocated transaction object or shared_ptr control block. Pass tx_ref instead to use the shared_ptr overload, which adds memusage::DynamicUsage(p), consistent with existing block and mempool-entry accounting. This leaves transaction deduplication and mempool exclusion unchanged. Update tx1_usage and tx2_usage in the unit test as well, since their dereferenced calls currently reproduce the omission. The result will still be allocator-dependent and conservative for combined make_shared allocations.


Coverage details

Earlier concerns

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