1 minor finding · 6 suggestions
Findings
[minor] · getMemoryLoad() can outlive the manager and mempool during shutdown
getMemoryLoad() extends an existing shutdown-lifetime hazard: an overlapping trusted IPC call can access the freed manager or mempool; no crash has been observed.
Location: src/node/interfaces.cpp:1059 (head).
Details and suggested fix
MinerImpl::getMemoryLoad() obtains a raw manager reference, and its scan repeatedly accesses m_mempool. The snapshot protects only the templates. Shutdown() calls disconnectIncoming() and then resets the manager and mempool, but libmultiprocess cancellation does not stop an implementation method already executing. It releases the cancellation lock during the method and checks cancellation afterward. A scan can therefore continue after disconnect returns and access either destroyed object. The retained parent connection also permits a later call to reach the null-manager Assert.
Stop accepting IPC Mining calls and wait for in-flight calls before resetting these dependencies. Retaining the manager alone is insufficient because it only references the mempool. The new cleanup-safety comment covers UnregisterTemplate() and destruction, not live calls; document this limitation if broader shutdown coordination remains separate.
Optional improvements
[suggestion] · wtxid membership hides a distinct transaction allocation retained by templates
The advisory estimate drops a template’s retained transaction after distinct-object mempool re-entry, although the original allocation remains alive; the new unit test requires this undercount.
Location: src/node/block_template_manager.cpp:122 (head).
Details and suggested fix
exists(tx_ref->GetWitnessHash()) checks hash membership, not whether the mempool shares the template’s object. Use CTxMemPool::get(const Wtxid&) and skip accounting only when the returned pointer equals tx_ref.get(). This preserves one mempool lock per unique transaction, at the cost of reference-count operations. The existing test already exercises distinct-object re-entry: CreateValidMempoolTransaction() submits its own object, assembly retains that object, and the test constructs a separate tx1 from the returned mutable transaction. After ProcessTransaction(tx1), the original remains in the templates. Update that expectation to all_overhead + tx1_usage + tx2_usage rather than requiring the acknowledged undercount. No limit currently depends on this estimate, and re-entry frequency in production has not been measured.
[suggestion] · Releasing retained templates performs quadratic registry work
Releasing N retained templates performs quadratic work under m_mutex, blocking registration and accounting snapshots during each scan; practical cleanup latency has not been measured.
Location: src/node/block_template_manager.cpp:71 (head).
Details and suggested fix
~BlockTemplateImpl() calls UnregisterTemplate(), which scans every registry entry and calls weak.lock() while holding m_mutex. Releasing accumulated templates, including through asynchronous disconnect cleanup, therefore visits about N(N+1)/2 entries. Use an std::unordered_map<const CBlockTemplate*, std::weak_ptr<const CBlockTemplate>> so ordinary unregister can erase by pointer in average constant time. The destructor still owns the template when removing its key, so this preserves non-owning tracking and the shutdown-safe weak-manager lookup. Expired-entry pruning, which SanityCheck() says should be unnecessary, can stay off the normal destruction path.
[suggestion] · getMemoryLoad() excludes more than the documented dummy coinbase
getMemoryLoad() excludes submitted coinbases and coinbase metadata, not just the documented dummy coinbase; no production client sending an unusually large coinbase was identified.
Location: src/node/block_template_manager.cpp:108 (head).
Details and suggested fix
submitSolution() replaces block.vtx[0] before validation, including on rejection, but accounting always skips that entry. The separate m_coinbase_tx.required_outputs vector and its witness-commitment script storage are also absent from the sum. Avoiding a race with coinbase replacement is valid, and the omitted memory is only a few hundred bytes per template in ordinary use. State the actual exclusions in mining.h, the manager’s accounting documentation, and the PR description: neither the coinbase transaction, whether dummy or submitted, nor its separate metadata is counted.
[suggestion] · SanityCheck() does not enforce its invariant in ordinary non-Debug tests
Without ABORT_ON_FAILED_ASSUME or a fuzz build, SanityCheck() cannot report expired registry entries, so the new accounting test misses a missing-unregister regression.
Location: src/node/block_template_manager.cpp:135 (head).
Details and suggested fix
Assume(!weak.expired()) evaluates the condition but does not fail in ordinary non-Debug builds. If destruction stopped unregistering a template, GetTemplateMemoryUsage() would skip its expired weak reference and the usage assertions would still pass. Replace this test-facing check with Assert(!weak.expired()) so the existing post-destruction SanityCheck() calls enforce the invariant in every build. Whether Debug-configured CI already exercises this test was not checked.
[suggestion] · Vector-overhead expectations reuse the accounting result under test
template_memory_usage derives its vector-overhead expectations from getMemoryLoad() itself, so omitting any non-vtx vector contribution would still satisfy its assertions.
Location: src/test/miner_tests.cpp:1022 (head).
Details and suggested fix
The test captures three overhead baselines from usage(), checks only that they are positive, and uses their sum in every later expectation. Removing the vTxFees, vTxSigOpsCost, or m_package_feerates term would shift those baselines consistently, while block.vtx would still provide positive overhead. Add a focused manager-level case with known allocation capacities in all four vectors and compare the reported overhead with their independently computed memusage::DynamicUsage sum. A dummy-only vtx can isolate the vector formula from transaction accounting. Keep the existing interface test for overlap, eviction, and lifetime behavior.
[suggestion] · The shutdown-lifetime test arrives after the cleanup change it validates
The lifetime test has no accounting dependency, so placing it with the tracking change would let reviewers validate shutdown-safe cleanup before proceeding through the accounting commits.
Location: src/test/miner_tests.cpp:971 at 1c80c9ca4cae (commit) (commit).
Details and suggested fix
4e57daf47e3baaa7690a631e60eb497cf0ed875e introduces the weak-manager lookup and guarded ~BlockTemplateImpl(), but block_template_manager_lifetime arrives in 1c80c9ca4cae02151f1dbdb47221bdb3b6dfd02e alongside accounting tests. Move that lifetime test into the tracking commit. Its MakeMining() helper and createNewBlock() call already exist there, and shared manager ownership comes from the preceding 5af61c441422fb5d1ce433c64b6f27dc7f4291b4. It uses neither GetTemplateMemoryUsage() nor getMemoryLoad(). This would put direct coverage beside the shutdown-cleanup claim and make that intermediate commit independently easier to validate; template_memory_usage can remain in the final test commit.