1 major finding · 1 minor finding · 1 suggestion
Findings
[major] · Template destruction can access the manager after shutdown resets it
If a trusted -ipcbind mining client retains templates during shutdown, asynchronous IPC cleanup can destroy them after or during the manager reset, causing an abort or access to freed memory.
Location: src/node/interfaces.cpp:893 (head).
Details and suggested fix
~BlockTemplateImpl() now unregisters through *Assert(m_node.block_template_manager). During shutdown, disconnectIncoming() waits for connection erasure, but ~ProxyServerBase() queues retained implementations for destruction on libmultiprocess's separate async cleanup thread. Nothing drains that cleanup before node.block_template_manager.reset(). If template destruction runs afterward, the assertion aborts; if it overlaps the reset, the unsynchronized pointer access can reach a manager being freed. This is scheduling-dependent and has not been reproduced. Calling destroy() before disconnecting avoids the path. Shutdown has already flushed chainstate here, so the established consequence is an unclean exit or undefined behavior, not data loss. Give each template shared ownership of a small registry and unregister through that registry rather than through NodeContext, so cleanup remains valid after manager teardown. Add a shutdown test whose client retains a template without calling destroy().
[minor] · Wtxid membership can hide a template-retained transaction allocation
When a template outlives removal and readmission of a transaction as a fresh object, getMemoryLoad() omits the old allocation retained by that template.
Location: src/node/block_template_manager.cpp:121 (head).
Details and suggested fix
The loop deduplicates by pointer but excludes by hash: exists(Wtxid) succeeds even when the mempool owns a different CTransaction allocation. Templates retain entry.GetSharedTx(), so readmitting identical bytes does not make their old allocation shared with the mempool again. This omission persists without concurrent mempool changes. The new test already constructs this distinction: CreateValidMempoolTransaction() submits its own allocation, while local tx1 is allocated separately and later reinserted. Its reentry assertion therefore expects the old allocation to disappear from usage. Compare allocation identity instead, using if (m_mempool.get(tx_ref->GetWitnessHash()) == tx_ref) continue;. This preserves the per-lookup lock and correctly excludes same-object reentry. Update the copy-reentry expectation and add a same-object reentry case using a reference from the template. If logical hash membership is intentional instead, document that distinction in getMemoryLoad(). This affects advisory accounting only; no production occurrence or frequency was found.
Optional improvements
[suggestion] · Each template release scans the whole registry
Releasing N retained templates sequentially performs N(N+1)/2 weak-pointer lock attempts under the registry mutex, also delaying other clients' registration and memory-load snapshots.
Location: src/node/block_template_manager.cpp:74 (head).
Details and suggested fix
UnregisterTemplate() runs std::erase_if across every registry entry and calls weak_ptr::lock() while holding m_mutex. Each template destructor invokes it, so releasing a retained batch through destroy() or disconnect cleanup produces quadratic aggregate work. createNewBlock() and successful waitNext() calls register templates without a count cap. Keep the registry non-owning, but key it by template address, for example with std::unordered_map<const CBlockTemplate*, std::weak_ptr<const CBlockTemplate>>, to give expected O(1) removal while preserving the linear snapshot. The practical cost depends on client retention and has not been measured. If the shutdown-lifetime correction removes explicit unregistration in favor of amortized lazy pruning, this concern disappears.