Skip to content

Conversation

@qu0b
Copy link
Contributor

@qu0b qu0b commented Jan 21, 2026

Summary

  • Fix race condition where libp2p bootstrap module connects to bootnodes before PeerManager registers its connectionOpen event listener
  • Add handleAlreadyConnectedPeers() method that checks for existing open connections after registering event listeners
  • Send STATUS to any outbound peers that were missed, ensuring compliance with the consensus spec requirement that "The dialing client MUST send a Status request upon connection"

Test plan

  • Build passes (yarn build)
  • Verify in Kurtosis testnet that Lodestar nodes properly send STATUS to Lighthouse bootnodes
  • Confirm no orphaned blocks at genesis due to missing peer connections

@qu0b qu0b requested a review from a team as a code owner January 21, 2026 10:09
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @qu0b, 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 race condition in the peer management system where libp2p's bootstrap module could establish connections with bootnodes before the PeerManager had registered its necessary event listeners. By introducing a mechanism to identify and process these early connections, the change ensures that all connected peers, especially outbound ones, correctly exchange STATUS messages, thereby upholding network protocol compliance and preventing potential issues with peer discovery and synchronization.

Highlights

  • Race Condition Fix: Introduced a new method, handleAlreadyConnectedPeers(), to address a race condition where peers might connect before the PeerManager is fully initialized and its event listeners are registered.
  • Consensus Compliance: Ensured that STATUS requests are sent to outbound peers that were connected prior to PeerManager initialization, maintaining compliance with the consensus specification requiring a Status request upon connection.
  • Early Connection Handling: Integrated the new peer handling logic into the PeerManager's startup sequence to proactively manage early connections established by the libp2p bootstrap module.

🧠 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.

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.

@qu0b qu0b changed the title qu0b: fix: handle peers connected before PeerManager init fix: qu0b handle peers connected before PeerManager init Jan 21, 2026
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 addresses a race condition where peers might connect before the PeerManager is fully initialized. The solution involves adding a handleAlreadyConnectedPeers method that is called during initialization to process any peers that connected early. The implementation looks solid and correctly handles the race condition. My main feedback is about code duplication between the new handleAlreadyConnectedPeers method and the existing onLibp2pPeerConnect handler. Refactoring this would improve maintainability and fix a minor inconsistency where the identify protocol is not run for these early-connected peers.

Fix race condition where libp2p bootstrap module connects to bootnodes
before PeerManager registers its connectionOpen event listener.

- Add handleAlreadyConnectedPeers() method that checks for existing open
  connections after registering event listeners
- Extract common peer tracking logic into trackNewPeer() helper to avoid
  code duplication between handleAlreadyConnectedPeers and onLibp2pPeerConnect
- Ensure STATUS is sent to outbound peers and identify protocol runs for
  all peers, maintaining compliance with the consensus spec requirement
  that "The dialing client MUST send a Status request upon connection"
@qu0b qu0b force-pushed the qu0b/fix-handle-already-connected-peers branch from aa07133 to 9b14e18 Compare January 21, 2026 10:13
@codecov
Copy link

codecov bot commented Jan 21, 2026

Codecov Report

❌ Patch coverage is 0% with 65 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (bal-devnet-2@5cda8b0). Learn more about missing BASE report.

Additional details and impacted files
@@               Coverage Diff               @@
##             bal-devnet-2    #8770   +/-   ##
===============================================
  Coverage                ?   52.20%           
===============================================
  Files                   ?      853           
  Lines                   ?    64834           
  Branches                ?     4772           
===============================================
  Hits                    ?    33848           
  Misses                  ?    30916           
  Partials                ?       70           
🚀 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.

@nflaig
Copy link
Member

nflaig commented Jan 22, 2026

@qu0b if this resolves your issue we can merge it into the bal-devnet branch, we can look into it more later to apply it to our unstable branch

@nflaig nflaig changed the title fix: qu0b handle peers connected before PeerManager init fix: handle peers connected before PeerManager init Jan 22, 2026
@qu0b
Copy link
Contributor Author

qu0b commented Jan 23, 2026

@nflaig ready for merge from my side

@nflaig nflaig merged commit 327fa80 into ChainSafe:bal-devnet-2 Jan 23, 2026
34 of 38 checks passed
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