-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix arbitrator deadlock found in ResolveContract
#10108
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
Fix arbitrator deadlock found in ResolveContract
#10108
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @yyforyongyu, 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 addresses a critical deadlock scenario within the contract court's arbitrator components. By refactoring the channel resolution signaling from a synchronous callback to an asynchronous channel-based notification, the potential for circular dependencies and blocking that led to the deadlock has been eliminated, improving the robustness of the system.
Highlights
- Deadlock Resolution: Implemented an asynchronous signaling mechanism to resolve a deadlock occurring when a
ChannelArbitratorattempts to stop while theChainArbitratoris resolving a contract. The previous synchronous call fromChannelArbitratortoChainArbitrator.ResolveContracthas been replaced. - Asynchronous Channel Resolution: Introduced a new
resolvedChan(lines 273-276) inChainArbitratorto receive asynchronous notifications fromChannelArbitratorwhen a channel has been fully resolved. TheChainArbitratornow processes these resolution signals in its main event loop (dispatchBlocks, lines 718-726), preventing direct synchronous calls that could lead to deadlocks. - API Change: The
ChannelArbitratorConfignow usesNotifyChannelResolved func()(lines 156-158 inchannel_arbitrator.go) instead ofMarkChannelResolved func() error, reflecting the change from a direct action to an asynchronous notification. TheChannelArbitratornow calls this new notification function (line 1396 inchannel_arbitrator.go) when a channel is resolved.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue 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 or fill out our survey to provide feedback.
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
-
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. ↩
There was a problem hiding this 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 provides a solid fix for the identified deadlock by decoupling the channel resolution notification from the contract resolution logic. The use of a channel to signal resolution asynchronously is a great approach. My review includes a few minor suggestions to align the code with the repository's style guide, primarily concerning line length for comments and log statements.
67d68e6 to
40a167e
Compare
Roasbeef
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still trying to understand how the dead lock occurs.
IIUC, this is only on shutdown? What exactly is the circular waiting condition? The resolver trying to call into the chain arb while it's shutting down which is what's calling shutdown?
| activeWatchers: make(map[wire.OutPoint]*chainWatcher), | ||
| chanSource: db, | ||
| quit: make(chan struct{}), | ||
| resolvedChan: make(chan wire.OutPoint), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this have a buffer of one?
Can we get a sequence flow explanation of how the deadlock could arise before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can guarantee that the ChainArb is always started before we call the ChannelArbs, so even without the buffer we are fine ?
|
After a bit of ultrathink, I understand the deadlock scenario now. See this diagram for a breakdown: https://gist.github.com/Roasbeef/4a3fbf28294eee98eca0143865981030#the-deadlock-scenario |
Roasbeef
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ziggie1984
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, nice fix 🎉
contractcourt/chain_arbitrator.go
Outdated
| // resolveContracts listens to the `resolvedChan` to mark a given channel as | ||
| // fully resolved. | ||
| func (c *ChainArbitrator) resolveContracts() { | ||
| // Consume block epochs until we receive the instruction to shutdown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Are we consuming block epochs here? Seems to me we only listen to the resolvedChan ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah wrong copy, now fixed!
| activeWatchers: make(map[wire.OutPoint]*chainWatcher), | ||
| chanSource: db, | ||
| quit: make(chan struct{}), | ||
| resolvedChan: make(chan wire.OutPoint), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can guarantee that the ChainArb is always started before we call the ChannelArbs, so even without the buffer we are fine ?
Thus we can mark channels as resolved in an async way to avoid deadlock.
40a167e to
19a6358
Compare
Fix the following deadlock,