Skip to content

Conversation

@ellemouton
Copy link
Collaborator

backport of #10420

The PubKey method had a race condition where concurrent calls could
all pass the nil check and race to write to the cached pubKey field.
This is a classic check-then-act race.

Remove the caching entirely to fix the race. The overhead of parsing
a public key is minimal and doesn't justify the added complexity and
race risk of caching.
@gemini-code-assist
Copy link

Summary of Changes

Hello @ellemouton, 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 backports critical fixes for several race conditions identified within the channel graph database. The changes primarily focus on enhancing thread safety by eliminating problematic caching patterns in public key parsing methods and introducing proper mutex protection for shared cache resources during block disconnection events. This ensures the integrity and consistency of the graph data under concurrent operations.

Highlights

  • Public Key Parsing Race Fix: Removed caching of parsed public keys in Node.PubKey(), ChannelEdgeInfo.NodeKey1(), and ChannelEdgeInfo.NodeKey2() methods to prevent check-then-act race conditions. These methods now parse the public key directly from the byte slice on each call.
  • Cache Access Race Fix: Implemented mutex locking (s.cacheMu.Lock() and s.cacheMu.Unlock()) around the rejectCache and chanCache access within the DisconnectBlockAtHeight function to ensure thread safety during channel disconnection events.
  • Backport: This pull request is a backport of an important race condition fix from the upstream repository, specifically addressing issues identified in https://github.com/lightningnetwork/lnd/pull/10420.
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

@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 is a backport that fixes several race conditions in the channel graph database. The changes remove racy check-then-act caching logic for public keys in Node and ChannelEdgeInfo models by parsing them from bytes on each call. Additionally, it adds proper mutex protection for cache access in DisconnectBlockAtHeight to prevent data races. The fixes are correct and improve the concurrency safety of the graph database. The release note update is also accurate.

@ellemouton ellemouton added this to the v0.20.1 milestone Dec 8, 2025
@ellemouton ellemouton self-assigned this Dec 8, 2025
Both NodeKey1 and NodeKey2 methods had the same race condition as the
Node.PubKey method, where concurrent calls could race to write to the
cached fields.

Remove the caching for the same reasons: parsing overhead is minimal
and doesn't justify the complexity and race risk.
The DisconnectBlockAtHeight method was modifying the rejectCache and
chanCache without holding the cacheMu lock. This caused races with
other operations that properly held the lock, such as AddChannelEdge
which modifies the caches in its OnCommit callback while the batch
scheduler holds cacheMu.

Fix by acquiring cacheMu before removing channels from the caches.
@ellemouton ellemouton force-pushed the backport-removePubKeyCaching branch from 0ddd1d1 to 7f09fd8 Compare December 8, 2025 12:22
@saubyk saubyk added the back port candidate pr which should be back ported to last major release label Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

back port candidate pr which should be back ported to last major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants