2 suggestions
Optional improvements
[suggestion] · blockToJSON and blockheaderToJSON lose their unlocked-entry guards
Removing the entry checks stops diagnosing JSON serialization under a caller-held cs_main, although no current caller was found entering either helper with that lock held.
Location: src/rpc/blockchain.h:42 (head).
Details and suggested fix
Base blockToJSON already combined LOCKS_EXCLUDED(cs_main) with brief internal metadata locks, so protecting nTx does not require relaxing entry. Because cs_main is recursive, the new WITH_LOCK can reacquire a caller-held lock without diagnosing it. That caller would then retain the global lock through block-size calculation, undo reads and per-transaction TxToUniv. Retain LOCKS_EXCLUDED(cs_main) on both declarations and AssertLockNotHeld(cs_main) in blockheaderToJSON, alongside the narrow nTx read. Reword the comment to distinguish unlocked entry from brief internal metadata locking.
[suggestion] · REST JSON headers reacquire cs_main for every header
With -rest enabled, JSON headers requests add up to 2,000 cs_main acquisitions during serialization; copying nTx during collection avoids them, although latency has not been measured.
Location: src/rpc/blockchain.cpp:212 (head).
Details and suggested fix
rest_headers defaults to five headers and collects the active-chain indices and tip under one lock. After releasing it, the JSON loop calls blockheaderToJSON once per returned header, triggering the new WITH_LOCK each time. Uncontended acquisition is cheap, but each round trip can wait on another cs_main holder. Copy each header's nTx alongside its index in the existing collection critical section and pass that value into the serializer. This preserves synchronization without moving JSON construction under the global lock. The binary and hex paths are unaffected.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
The historical objective was to keep expensive block-serving work outside cs_main so independent requests could execute concurrently rather than serialize behind the global lock.
Historical PR #26308 explicitly reduced cs_main scope in blockToJSON and related RPC/REST paths. Its author reported substantial parallel-request speedups and identified JSON serialization as a significant remaining cost for verbose getblock. This supports distinguishing a short protected metadata read from holding cs_main throughout JSON construction; the historical performance results do not measure the current patch.
Ralph's inferences
The original contract is best understood as a caller-side critical-section boundary: obtain the necessary chain references under cs_main, release it, and serialize against the supplied tip rather than repeatedly consult the changing active chain.
The pinned base explicitly says to serialize passed information without accessing active-chain state and asserts that cs_main is not held 'For performance reasons.' The inspected getblockheader caller captures the block index and active tip under cs_main, then calls blockheaderToJSON after releasing it. ComputeNextBlockAndDepth uses that supplied tip. Together these observations explain the caller assumption as avoiding a long serialization critical section, consistent with the independently documented lock-scope objective.
A prohibition on entering these helpers while holding cs_main is not equivalent to a prohibition on short internal acquisitions. The historical performance rationale therefore constrains lock duration and caller behavior more directly than it constrains internal acquisition itself.
Before this patch, blockToJSON already briefly acquires cs_main internally for pruning and undo-availability metadata despite its LOCKS_EXCLUDED declaration and the assertion reached through blockheaderToJSON. The head adds another narrow acquisition for nTx, while removing the declarations and assertion. Thus two distinct properties change: synchronizing a metadata read and no longer enforcing unlocked entry. The inspected history explains why unlocked entry mattered for performance, but does not establish that internal acquisition violates an original lock-order requirement or that removing the entry guard preserves every caller assumption.