Skip to content

fix: yield to event loop between block imports during sync#8925

Merged
nflaig merged 1 commit intoChainSafe:unstablefrom
lodekeeper:fix/yield-between-block-imports
Feb 17, 2026
Merged

fix: yield to event loop between block imports during sync#8925
nflaig merged 1 commit intoChainSafe:unstablefrom
lodekeeper:fix/yield-between-block-imports

Conversation

@lodekeeper
Copy link
Contributor

Description

During finalized sync, blocks are imported in a tight loop without yielding to the event loop. When BLS verification is async (not awaiting the execution engine's newPayload), this prevents the checkpoint state cache's processState() cleanup from running, causing unbounded state accumulation and OOM on memory-constrained hosts.

This adds nextEventLoop() after each importBlock to allow pending async cleanup (state serialization, cache eviction) to execute between block imports.

Root Cause

In processBlocks(), the import loop previously had a natural yield point when importBlock awaited the execution engine. With async BLS via @chainsafe/blst (PR #8900), importBlock no longer blocks on the EL call, so blocks blast through without yielding. The checkpoint state cache's processState() — which runs as fire-and-forget async — never gets a chance to serialize and evict old states, causing memory to climb until OOM.

Fix

for (const fullyVerifiedBlock of fullyVerifiedBlocks) {
  await importBlock.call(this, fullyVerifiedBlock, opts);
  await nextEventLoop(); // Allow processState() cleanup to run
}

nextEventLoop() is an existing utility (sleep(0)) that yields to the event loop's timers phase, giving pending microtasks and macrotasks a chance to execute.

Testing

Deployed as commit 983d923 on feat3 infrastructure nodes — resolved OOM crash loops on nodes with sufficient memory. Memory-constrained hosts (~16GB) may need additional tuning (reduced maxBlockStates, queue depth limits).

Co-authored-by: Cayman caymannava@gmail.com


Note

This PR was authored with AI assistance (Claude). All code has been reviewed and validated.

During finalized sync, blocks are imported in a tight loop without yielding
to the event loop. With async BLS verification (no EL newPayload await),
this prevents the checkpoint state cache processState() cleanup from running,
causing unbounded state accumulation and OOM on memory-constrained hosts.

Add nextEventLoop() after each importBlock to allow pending async cleanup
(state serialization, cache eviction) to execute between block imports.

Co-authored-by: Cayman <caymannava@gmail.com>
@lodekeeper lodekeeper requested a review from a team as a code owner February 17, 2026 19:47
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lodekeeper, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical Out-Of-Memory (OOM) issue that occurred during finalized block synchronization, particularly on systems with limited memory. By strategically yielding control to the event loop after each block import, it ensures that essential asynchronous cleanup and state management processes can execute, preventing memory exhaustion and improving the stability of the beacon node.

Highlights

  • Memory Management during Sync: Introduced nextEventLoop() after each importBlock call during finalized sync to prevent unbounded state accumulation and Out-Of-Memory (OOM) errors on memory-constrained hosts.
  • Event Loop Yielding: Ensured the event loop yields between block imports, allowing asynchronous cleanup tasks (like state serialization and cache eviction by processState()) to execute.
  • Root Cause Addressed: Fixed an issue where async BLS verification (introduced in PR feat(blst): replace blst and pubkeys with lodestar-z #8900) removed a natural yield point, causing importBlock to no longer block on the execution engine and preventing cleanup.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • packages/beacon-node/src/chain/blocks/index.ts
    • Imported nextEventLoop utility.
    • Added await nextEventLoop() call within the block import loop in processBlocks to allow event loop tasks to run.
    • Removed a comment indicating no need for sleep(0).
Activity
  • The change was deployed as commit 983d923f on feat3 infrastructure nodes for testing.
  • Testing confirmed that the fix resolved OOM crash loops on nodes with sufficient memory.
  • The PR was co-authored by Cayman (caymannava@gmail.com).
  • The PR was authored with AI assistance (Claude), with all code reviewed and validated.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a critical out-of-memory issue during finalized sync by introducing a yield to the event loop between block imports. This change is crucial for allowing asynchronous cleanup processes, such as state serialization and cache eviction, to execute, thereby preventing unbounded memory accumulation. The solution is well-explained and directly targets the identified root cause.

);

for (const fullyVerifiedBlock of fullyVerifiedBlocks) {
// No need to sleep(0) here since `importBlock` includes a disk write
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The removal of this comment is appropriate. The previous statement about importBlock including a disk write and thus not needing sleep(0) is no longer accurate given the asynchronous BLS verification and the explicit addition of await nextEventLoop() to yield to the event loop. This improves code clarity by removing potentially misleading information.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right — the old comment assumed importBlock always included a blocking disk write, which was true before #8784 moved verification async. That natural yield point is now gone, so the explicit nextEventLoop() replaces it.

@nflaig nflaig merged commit 983b1a4 into ChainSafe:unstable Feb 17, 2026
19 checks passed
@codecov
Copy link

codecov bot commented Feb 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.33%. Comparing base (e1d3398) to head (1951431).
⚠️ Report is 1 commits behind head on unstable.

Additional details and impacted files
@@            Coverage Diff            @@
##           unstable    #8925   +/-   ##
=========================================
  Coverage     52.33%   52.33%           
=========================================
  Files           848      848           
  Lines         63429    63429           
  Branches       4702     4702           
=========================================
  Hits          33195    33195           
  Misses        30165    30165           
  Partials         69       69           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

lodekeeper added a commit to lodekeeper/lodestar that referenced this pull request Feb 20, 2026
During test teardown (and production shutdown), the callInNextEventLoop
callback for producing common block body can fire after the chain's
abort signal has triggered. This causes getBlockSlotState to throw
QUEUE_ERROR_QUEUE_ABORTED, which propagates as an unhandled promise
rejection since no one is awaiting the deferred.

This fix:
1. Guards against calling produceCommonBlockBody after the controller
   signal has been aborted
2. Silently handles queue abort errors during shutdown instead of
   propagating them as unhandled rejections
3. Increases prover E2E hook timeout from 3 to 4 epochs of headroom
   to accommodate slower CI runners and block import yields (ChainSafe#8925)

Fixes flaky E2E CI failures where all 44 test files pass but Vitest
exits with code 1 due to the unhandled rejection.
lodekeeper added a commit to lodekeeper/lodestar that referenced this pull request Feb 20, 2026
During test teardown (and production shutdown), the callInNextEventLoop
callback for producing common block body can fire after the chain's
abort signal has triggered. This causes getBlockSlotState to throw
QUEUE_ERROR_QUEUE_ABORTED, which propagates as an unhandled promise
rejection since no one is awaiting the deferred.

This fix:
1. Guards against calling produceCommonBlockBody after the controller
   signal has been aborted
2. Silently handles queue abort errors during shutdown instead of
   propagating them as unhandled rejections
3. Increases prover E2E hook timeout from 3 to 4 epochs of headroom
   to accommodate slower CI runners and block import yields (ChainSafe#8925)

Fixes flaky E2E CI failures where all 44 test files pass but Vitest
exits with code 1 due to the unhandled rejection.
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.

3 participants