Skip to content

fix: await processFn to prevent buffer pool race condition#8877

Merged
nflaig merged 1 commit intoChainSafe:unstablefrom
lodekeeper:fix/buffer-pool-race-condition
Feb 7, 2026
Merged

fix: await processFn to prevent buffer pool race condition#8877
nflaig merged 1 commit intoChainSafe:unstablefrom
lodekeeper:fix/buffer-pool-race-condition

Conversation

@lodekeeper
Copy link
Contributor

Motivation

Fixes a race condition that can cause state corruption and First offset must equal to fixedEnd errors on restart.

See discussion: https://discord.com/channels/593655374469660673/1469368525180113078

Description

The using keyword in serializeState.ts releases the buffer back to the pool when the block exits. Since processFn is async (returns a Promise), the buffer was being released before the DB write completed.

If another serialization (checkpoint state or archive state) happened before the write finished, it would:

  1. Get the same buffer from the pool
  2. Call fill(0) on it (per BufferPool.alloc behavior)
  3. Corrupt the data being written by the first serialization

This could cause First offset must equal to fixedEnd 0 != <large number> errors on restart when the corrupted state is read.

Fix

Add await before processFn(stateBytes) to ensure the buffer is not released until the async operation completes.


AI Disclosure: This PR was authored with AI assistance (Lodekeeper/Claude).

The `using` keyword releases the buffer back to the pool when the block
exits. Since processFn is async (returns a Promise), the buffer was being
released before the DB write completed.

If another serialization (checkpoint state or archive state) happened
before the write finished, it would get the same buffer and call fill(0),
corrupting the in-flight write.

This could cause 'First offset must equal to fixedEnd' errors on restart
when the corrupted state is read from the database.
@lodekeeper lodekeeper requested a review from a team as a code owner February 6, 2026 19:15
@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 significant data integrity issue by correcting a race condition during state serialization. By ensuring that buffers are not returned to the pool until all asynchronous write operations are complete, it prevents potential state corruption and system errors, enhancing the stability and reliability of the application.

Highlights

  • Race Condition Fix: Addressed a critical race condition in state serialization that could lead to state corruption and 'First offset must equal to fixedEnd' errors upon system restart.
  • Root Cause Identified: The issue stemmed from a buffer being prematurely released back to the buffer pool by the using keyword in serializeState.ts because the processFn was an async operation that was not awaited, allowing another serialization to reuse and corrupt the buffer before the initial write completed.
  • Solution Implemented: The fix involves adding an await keyword before processFn(stateBytes) to ensure that the buffer is not released until the asynchronous database write operation has fully completed, thereby preventing data corruption.

🧠 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/serializeState.ts
    • Modified the serializeState function to await the processFn call.
    • Added a comment explaining the necessity of await to prevent premature buffer release.
Activity
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 resolves a critical race condition within the serializeState function. By adding await before processFn(stateBytes), the buffer is now guaranteed to not be released back to the pool until the asynchronous operation completes. This directly prevents the state corruption and First offset must equal to fixedEnd errors described in the motivation. The added comment provides excellent clarity on the purpose of this crucial change, significantly improving code maintainability and understanding for future developers.

@nflaig
Copy link
Member

nflaig commented Feb 7, 2026

going to merge this so that we can cut another rc

@nflaig nflaig merged commit b87b37f into ChainSafe:unstable Feb 7, 2026
24 of 26 checks passed
@codecov
Copy link

codecov bot commented Feb 7, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##           unstable    #8877   +/-   ##
=========================================
  Coverage     52.34%   52.34%           
=========================================
  Files           848      848           
  Lines         63488    63487    -1     
  Branches       4704     4704           
=========================================
  Hits          33230    33230           
+ Misses        30189    30188    -1     
  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.

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.

2 participants