Skip to content

fix(blockchain): discard a pending subtree with a worklist, not a call per descendant - #955

Open
pablodeymo wants to merge 1 commit into
mainfrom
fix/810-iterative-discard-pending-subtree
Open

pablodeymo wants to merge 1 commit into
mainfrom
fix/810-iterative-discard-pending-subtree

Conversation

@pablodeymo

Copy link
Copy Markdown
Collaborator

🗒️ Description / Motivation

BlockChainServer::discard_pending_subtree called itself once for every pending child. It runs on the chain actor's thread (Backend::Thread, a std::thread with the default 2 MiB stack), and nothing caps how deep a chain of pending blocks gets, so a deep enough chain overflows the stack and aborts the process (#810).

The issue left three things unmeasured. This PR answers them:

Question in #810 Answer
Can such a chain form? Yes, from blocks with no valid signature. A block by a proposer the head state lacks waits for its parent unsigned (precheck_before_waiting, UnknownProposer under Wait::Parent), and a parked block's slot is never compared with its child's, so the depth is not bounded by the unfinalized window. The new test parks 50,000 blocks at one slot this way, through process_or_pend_block
What triggers the discard? A block at or below the finalized slot arriving with the chain parked on it
At what depth does it overflow? About 2,200 parked blocks under release-fast (roughly 940 bytes a level) and about 12,700 under release (roughly 164 bytes), bisected with the new test on the old code. Measured on macOS aarch64 in the crate's test binary; the shipped binary may differ

Intended for release/v2.0.0 by cherry-pick once merged here.

What Changed

crates/blockchain/src/lib.rs only.

  • discard_pending_subtree keeps the roots still to discard in a Vec and loops, in place of the self-call. The per-root cleanup is the same code, re-indented: the diff is small with whitespace hidden (?w=1).
  • Two tests, beside the other parked-block tests.

Correctness / Behavior Guarantees

  • The same roots get the same cleanup as before (blocks_awaiting_columns, the parent-payload hold, envelopes awaiting import, held_timings, pending_blocks, pending_block_parents). Only the order changes, and no step reads what another root's step wrote.
  • Not changed: pending_blocks is still uncapped, so a peer can still make the node hold many parked blocks. That is a memory bound, a separate matter from this crash.

Tests Added / Run

  • a_parked_chain_is_discarded_whole_without_overflowing_a_default_stack: parks a 50,000-block chain through process_or_pend_block (unsigned blocks, tip first, all at one slot) on a thread with an explicit 2 MiB stack, delivers the block at the finalized slot, and checks pending_blocks, pending_block_parents and held_timings are empty. On the old code it aborts with has overflowed its stack under both profiles. It runs in about 0.3 s.
  • a_discard_takes_every_branch_and_leaves_other_parked_chains: a branching subtree goes whole and a chain parked elsewhere stays. Checked to fail when the walk stops at the first childless block.
  • cargo test -p ethlambda-blockchain --profile release-fast --lib: 427 passed, 46 ignored (the fixture-gated ones).
  • cargo clippy -p ethlambda-blockchain --all-targets -- -D warnings and cargo fmt --all -- --check: clean.
  • Not run: the workspace-wide make lint and make test.
  • Not tested: a peer driving this over the wire. The test enters at process_or_pend_block, not at the p2p layer. Each parked block costs the peer one answered by-root request, and a retry picks a peer at random, so how fast a single peer can build the chain is not measured.

Related Issues / PRs

✅ Verification Checklist

  • Ran make fmt — clean
  • Ran make lint (clippy with -D warnings) — clean (only ethlambda-blockchain)
  • Ran make test (test-consensus, test-node and test-ethrex, at release-fast) — all passing (only ethlambda-blockchain --lib)

…l per descendant

`discard_pending_subtree` called itself for every pending child, on the chain
actor's thread (2 MiB stack), and nothing caps how deep a chain of pending
blocks gets. A block by a proposer the head state lacks waits for its parent
unsigned, and a parked block's slot is not compared with its child's, so the
depth is not bounded by the unfinalized window either. A block at or below
the finalized slot then discards everything parked on it.

Measured with the new test on the old code: the stack overflows, aborting
the process, about 2,200 parked blocks down under `release-fast` and about
12,700 under `release`.

The walk now keeps the roots still to discard in a `Vec`. The per-root
cleanup is unchanged.

Closes #810
@pablodeymo

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

Copy link
Copy Markdown

AI security review: 0 finding(s) to check

Reviewed by Opus, GPT, Kimi, GLM, DeepSeek.

No findings to check.

Cost of this review: ~$0.94 of a $30 budget (estimated from token counts).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

blockchain: discard_pending_subtree recurses once per pending descendant

1 participant