Skip to content

Comments

backport[0.20.x]: graph/db: fix SetSourceNode no rows error#10418

Merged
yyforyongyu merged 4 commits intolightningnetwork:v0.20.x-branchfrom
ellemouton:fixSourceNodeSetting-v0.20.x
Dec 4, 2025
Merged

backport[0.20.x]: graph/db: fix SetSourceNode no rows error#10418
yyforyongyu merged 4 commits intolightningnetwork:v0.20.x-branchfrom
ellemouton:fixSourceNodeSetting-v0.20.x

Conversation

@ellemouton
Copy link
Collaborator

backport: #10371

@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 a critical fix to resolve a race condition that could hinder a node's startup process. The issue arose when multiple internal processes attempted to update the node's own announcement in the database simultaneously, leading to failures if timestamps were not strictly increasing. The solution involves implementing a more lenient upsert operation specifically for the source node, ensuring that all parameter changes persist even when concurrent updates occur with the same timestamp, thereby improving node stability and reliability.

Highlights

  • Race Condition Fix: Addresses a race condition in the SetSourceNode function that could prevent a node from starting up if multiple goroutines attempted to update the node's announcement concurrently with the same timestamp.
  • New Upsert Mechanism for Source Node: Introduces a dedicated UpsertSourceNode SQL query and corresponding Go function. This new upsert is less strict about timestamp checks, allowing updates to the source node even if the last_update timestamp has not strictly increased, which is crucial for handling concurrent self-node updates.
  • Refactored Node Upsert Logic: Refactors the node upsert logic by extracting common ancillary data updates and parameter building into helper functions, improving code organization and reusability for both general and source node upserts.
  • New Test Case: Adds TestSetSourceNodeSameTimestamp to verify that the SetSourceNode function correctly handles updates with identical timestamps for the source node, ensuring the fix behaves as expected under race conditions.
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 backports a fix for a race condition that could occur when updating the source node. The fix introduces a new, less strict SQL upsert query (UpsertSourceNode) that allows updates with the same timestamp, resolving the issue of concurrent updates failing. A new test case, TestSetSourceNodeSameTimestamp, is added to verify this behavior. The changes also include a refactoring of the node upsert logic in graph/db/sql_store.go to reduce code duplication and improve maintainability. The fix is correct and the changes are well-implemented. I have one suggestion to further improve the readability of the refactored code.

This commit adds TestSetSourceNodeSameTimestamp to demonstrate the
current behavior when SetSourceNode is called with the same last update
timestamp. The test reveals a difference between the SQL and bbolt
implementations:

- SQL store returns sql.ErrNoRows when attempting to update with the
  same timestamp, as the upsert query's UPDATE clause requires the new
  timestamp to be strictly greater than the existing one
- bbolt store silently ignores stale updates and returns no error

This behavior is important to document because our own node
announcements may change quickly with the same timestamp, unlike
announcements from other nodes where same timestamp typically means
identical parameters.
This query is less strict in terms of the latest update timestamp field.
We want to be less strict with our own node data since we always want
our own updates recorded.
This commit fixes a race condition where multiple goroutines call
SetSourceNode concurrently during startup, causing sql.ErrNoRows
errors. The race occurs when multiple code paths (setSelfNode,
createNewHiddenService, RPC updates) read the same old timestamp,
independently increment it to the same new value (T+1), and race to
write.

The fix uses the new UpsertSourceNode SQL query (without strict
timestamp constraint) instead of UpsertNode. This allows
last-write-wins semantics for our own node, ensuring all parameter
changes persist even when timestamps collide.

Refactored sql_store.go for reusability:
- upsertNodeAncillaryData: common logic for features/addresses/extras
- populateNodeParams: common parameter building with callback pattern
- buildNodeUpsertParams: builds params for strict UpsertNode
- buildSourceNodeUpsertParams: builds params for lenient UpsertSourceNode
- upsertSourceNode: new function using lenient query

Updated TestSetSourceNodeSameTimestamp to verify that concurrent
updates with the same timestamp now succeed and parameter changes
persist.

Fixes the itest error:
"unable to upsert source node: upserting node(...): sql: no rows in
result set"
@ellemouton ellemouton force-pushed the fixSourceNodeSetting-v0.20.x branch from 724bf99 to 2590593 Compare December 4, 2025 10:22
Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

Thanks!

@yyforyongyu yyforyongyu merged commit 5bfcdf9 into lightningnetwork:v0.20.x-branch Dec 4, 2025
37 of 39 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