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

lnd: allow shutdown signal during IsSynced check #9137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ViktorTigerstrom
Copy link
Collaborator

Fixes #9109

Before this PR, lnd could become unresponsive to shutdown signals during the IsSynced check, which can sometimes take a long time to complete. This caused delays in shutting down lnd.

I'm pushing this as a draft PR for now, as there are several possible solutions to consider:

  1. Cancelable context: The cleanest approach might be to pass a cancelable context into the IsSynced function, allowing it to exit immediately when a shutdown signal is received. However, this would require updates to both the btcwallet and btcd repositories, and we would need to wait for new releases with these changes. There are also two activeChainControl.ChainIO.GetBestBlock() calls in lnd.go—just before and after this PR’s changes—which could block as well, though we haven't seen extended delays in those calls.

  2. Listening to async channels: Another option is to modify the IsSynced function to listen to the underlying async channels from GetBestBlock and GetBlockHeader. This would also require changes to the btcwallet repo and a release.

To reviewers: I would appreciate feedback on which approach you think is best. :)

Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

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.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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.

Copy link
Collaborator

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

Approach ack.

lnd.go Outdated
Err error
}

syncCheckLoop:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: would personally prefer not using syncCheckLoop and just do break

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! The reason for the labelled loop is that just using a break would've just cancelled the select statement and not the outer loop.

I refactored this a bit though so that just canceling the select statement works fine :)! I also prefer the new version without the syncCheckLoop label, as it makes it a bit clearer what the actual loop is for.

lnd.go Outdated Show resolved Hide resolved
lnd.go Outdated
// If we're not yet synced, we'll wait for a second before
// checking again.
select {
case <-interceptor.ShutdownChannel():
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like we don't need to catch it again here since after 1s it will go to the next iteration.

Copy link
Collaborator Author

@ViktorTigerstrom ViktorTigerstrom Sep 30, 2024

Choose a reason for hiding this comment

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

I still included this change, as we'll else block the shutdown for 1 second while waiting for this. If you think that's an unnecessary optimisation, I'm ok with removing it though and just keep the:
<-time.After(time.Second)

@ViktorTigerstrom
Copy link
Collaborator Author

Thanks a lot for the review @yyforyongyu 🙏! Given the Approach ack, I'll take this out of draft mode :)

@ViktorTigerstrom ViktorTigerstrom marked this pull request as ready for review September 30, 2024 11:44
Copy link
Collaborator

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

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

tACK 🔥

A few style suggestions but otherwise lgtm!

lnd.go Outdated
Comment on lines 671 to 673
IsSynced bool
TS int64
Err error
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: do these need to be exported members?

Copy link
Collaborator

Choose a reason for hiding this comment

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

also suggest:

	        synced    bool
		bestBlock int64
		err       error

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks 🙏! Went with your suggestion :). I renamed the TS to bestBlockTime though, to make it easily differentiable from the bestHeight variable above. Hope that's ok!

lnd.go Outdated Show resolved Hide resolved
lnd.go Outdated
syncedResChan := make(chan syncResult, 1)
isSynced := false

for !isSynced {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can get away without the isSynced variable. Something like:

        var syncedResChan = make(chan syncResult, 1)
	for {
		// We check if the wallet is synced in a separate goroutine as
		// the call is blocking, and we want to be able to interrupt it
		// if the daemon is shutting down.
		go func() {
			synced, bestBlock, err := activeChainControl.Wallet.
				IsSynced()
			syncedResChan <- syncResult{synced, bestBlock, err}
		}()

		select {
		case <-interceptor.ShutdownChannel():
			return nil

		case res := <-syncedResChan:
			if res.err != nil {
				return mkErr("unable to determine if wallet "+
					"is synced: %v", res.err)
			}

			if res.synced {
				break
			}

			// If we're not yet synced, we'll wait for a second
			// before checking again.
			select {
			case <-interceptor.ShutdownChannel():
				return nil

			case <-time.After(time.Second):
				continue
			}
		}

		break
         }
	

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! Updated the PR to your suggestion 🚀. I'm in sight favour of the isSynced variable approach though as I think that's a bit more readable, but have no strong opinion :). I did however keep the "Syncing to block timestamp" log line, as I do think that adds value for users.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I did however keep the "Syncing to block timestamp" log line, as I do think that adds value for users.

yup - was just posting pseudo code to show the design, not an exact "pls copy as is" snippet

Prior to this commit, lnd could become unresponsive to shutdown signals
during the `IsSynced` check. Since the `IsSynced` check can occasionally
take a long time to complete, this could result in lnd failing to shut
down promptly.
@ViktorTigerstrom
Copy link
Collaborator Author

Thanks a lot for the review @ellemouton 🙏! I've addressed your feedback :)

@lightninglabs-deploy
Copy link

@yyforyongyu: review reminder
@ViktorTigerstrom, remember to re-request review from reviewers when ready

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.

4 participants