Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discovery: obtain channelMtx before doing any DB calls in handleChannelUpdate #9573

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ellemouton
Copy link
Collaborator

There exists an edge-case where we receive the same ChannelUpdate from 2 different peers
in quick succession. Both will make it past the initial IsStale check since neither have been
persisted yet, this means that both updates will count towards the rate limit count for that peer:channel
combo.

To fix this, we need to re-check the staleness of the update once the lock is acquired.

This fixes the flake seen in this build: https://github.com/lightningnetwork/lnd/actions/runs/13583650550/job/37973971226

Copy link
Contributor

coderabbitai bot commented Mar 3, 2025

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ellemouton ellemouton force-pushed the checkUpdateStalenessBeforeRateLimit branch from d16e005 to c3792a3 Compare March 3, 2025 12:16
@ellemouton ellemouton self-assigned this Mar 3, 2025
@ellemouton ellemouton force-pushed the checkUpdateStalenessBeforeRateLimit branch 2 times, most recently from 95608c3 to 355e6db Compare March 3, 2025 15:01
@@ -381,7 +452,11 @@ func (r *mockGraphSource) IsStaleEdgePolicy(chanID lnwire.ShortChannelID,
// NOTE: This method is part of the ChannelGraphSource interface.
func (r *mockGraphSource) MarkEdgeLive(chanID lnwire.ShortChannelID) error {
r.mu.Lock()
defer r.mu.Unlock()
defer func() {
r.callCount["MarkEdgeLive"]++
Copy link
Member

Choose a reason for hiding this comment

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

An alternative way to accomplish this would be to use mock.Mock. Then you can just do mock.AssertNumberOfCalls(t, "MarkEdgeLive", 3). You can also set the expectations upfront as well.

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
Member

Choose a reason for hiding this comment

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

yeah it would be great if we can gradually replace all mockers using mock.Mock, tho that would involve quite some refactor of the tests here. So maybe a PR at the end of the graph cleanup series?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So maybe a PR at the end of the graph cleanup series?

Yeah sounds like a plan 🫡

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 for the PR! I thought our rate limiter is per channel per peer, but it seems like it's per channel - does it mean an update from peer A can take up peer B's slot?

In addition this seems new,

--- FAIL: TestLightningNetworkDaemon/tranche04/142-of-271/bitcoind/relayer_blinded_error (134.58s)
        harness_node.go:403: Starting node (name=Alice) with PID=12111
        harness_node.go:403: Starting node (name=Bob) with PID=13149
        harness_node.go:403: Starting node (name=Carol) with PID=14105
        harness_node.go:403: Starting node (name=Dave) with PID=14327
        harness_assertion.go:2331: 
            	Error Trace:	/home/runner/work/lnd/lnd/lntest/harness_assertion.go:2331
            	            				/home/runner/work/lnd/lnd/itest/lnd_route_blinding_test.go:682
            	            				/home/runner/work/lnd/lnd/lntest/harness.go:297
            	            				/home/runner/work/lnd/lnd/itest/lnd_test.go:130
            	Error:      	[]routerrpc.HtlcEvent_EventType{3, 0} does not contain 1
            	Test:       	TestLightningNetworkDaemon/tranche04/142-of-271/bitcoind/relayer_blinded_error
            	Messages:   	wrong event type, got FORWARD%!(EXTRA routerrpc.HtlcEvent_EventType=SEND)
        harness.go:375: finished test: relayer_blinded_error, start height=541, end height=548, mined blocks=7
        harness.go:334: test failed, skipped cleanup
    lnd_test.go:138: Failure time: 2025-03-03 15:15:52.959

@@ -381,7 +452,11 @@ func (r *mockGraphSource) IsStaleEdgePolicy(chanID lnwire.ShortChannelID,
// NOTE: This method is part of the ChannelGraphSource interface.
func (r *mockGraphSource) MarkEdgeLive(chanID lnwire.ShortChannelID) error {
r.mu.Lock()
defer r.mu.Unlock()
defer func() {
r.callCount["MarkEdgeLive"]++
Copy link
Member

Choose a reason for hiding this comment

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

yeah it would be great if we can gradually replace all mockers using mock.Mock, tho that would involve quite some refactor of the tests here. So maybe a PR at the end of the graph cleanup series?

@ellemouton
Copy link
Collaborator Author

ellemouton commented Mar 5, 2025

I thought our rate limiter is per channel per peer, but it seems like it's per channel - does it mean an update from peer A can take up peer B's slot?

It is per-channel-per-peer

In addition this seems new,

Yeah saw that but just cant think that it is related to this PR, so assumed it was a rare flake. but will look at the logs a bit 👍 EDIT: damn, looks like the logs have expired/were not uploaded

@ellemouton ellemouton force-pushed the checkUpdateStalenessBeforeRateLimit branch 2 times, most recently from d447fe5 to 1a9f269 Compare March 5, 2025 05:48
@ellemouton ellemouton changed the title discovery: re-check ChannelUpdate staleness after acquiring the channelMtx discovery: obtain channelMtx before doing any DB calls in handleChannelUpdate Mar 5, 2025
@ellemouton ellemouton requested a review from yyforyongyu March 5, 2025 07:12
Copy link
Collaborator

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM, very nice test demonstrating the bug! 🥇

This commit adds a test to demonstrate that if we receive two identical
updates (which can happen if we get the same update from two peers in
quick succession), then our rate limiting logic will be hit early as
both updates might be counted towards the rate limit. This will be fixed
in an upcoming commit.
In `handleChanUpdate`, make sure to grab the `channelMtx` lock before
making any DB calls so that the logic remains consistent.
@ellemouton ellemouton force-pushed the checkUpdateStalenessBeforeRateLimit branch from 1a9f269 to 95277bb Compare March 5, 2025 12:13
@ellemouton
Copy link
Collaborator Author

ellemouton commented Mar 5, 2025

~looking into the failing itest 👍 ~

nvm, looks like a known flake:

// NOTE: this test can be flaky as we are testing the chan-enable-timeout and

@ellemouton ellemouton requested a review from yyforyongyu March 5, 2025 13:16
@yyforyongyu
Copy link
Member

// NOTE: this test can be flaky as we are testing the chan-enable-timeout and

That describes the flake found in updating channels and asserting the channel disabled status, but the failure was the node couldn't sync to graph, so I think it's something different. I prolly should update the desc there - initially I want people to know the test can be flaky if the params were not carefully crafted, which led to the specific params I used in that test, and that removed the flakeness.

Looking into the logs, I found this error,

2025-03-05 12:20:21.330 [ERR] DISC gossiper.go:873: Process query msg from peer [Alice] got unexpected msg *lnwire.ReplyShortChanIDsEnd received in state queryNewChannels

Not sure if it's related to the change here or just a flake, tho I think in general msgs should be sent in order, need to double check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants