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

Don't drop ChannelMonitor Events until they're processed #2369

Merged

Conversation

TheBlueMatt
Copy link
Collaborator

@TheBlueMatt TheBlueMatt commented Jun 20, 2023

We currently assume the owner of ChannelMonitors won't persist
the ChannelMonitor while Events are being processed. This is
fine, except (a) its generally hard to do so and (b) the
ChainMonitor doesn't even do this.

Thus, in rare cases, a user could begin processing events which
are, generated by connecting a transaction or a new best-block,
take some time to do so, and while doing so process a further chain
event, causing persistece. This could lose the event being
processed alltogether, which could lose the user funds.

This should be very rare, but may have been made slightly more
reachable with (a) the async event processing making it more
common to do networking in event handling, (b) the new future
generation in the ChainMonitor, which now wakes the
background-processor directly when chain actions happen on the
ChainMonitor.

Based on #2367

@TheBlueMatt TheBlueMatt added this to the 0.0.116 milestone Jun 20, 2023
@TheBlueMatt
Copy link
Collaborator Author

Rebased.

lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Show resolved Hide resolved
for funding_txo in mons_to_process {
let mut ev;
super::channelmonitor::process_events_body!(
self.monitors.read().unwrap().get(&funding_txo).map(|m| &m.monitor), ev, handler(ev).await);
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this first argument ever be None?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently I don't think so, but in the future we probably want to support removing monitors after they're done with all their processing (and probably will do so automatically). Thus, we should handle the case where the monitor has dissapeared on us (we don't hold a lock through the loop anyway, so its good practice to not rely on any assumptions about the underlying data changing).

@TheBlueMatt TheBlueMatt force-pushed the 2023-06-mon-event-less-race branch 2 times, most recently from 669280e to 3fc31fe Compare July 7, 2023 15:20
Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

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

Largely LGTM

@codecov-commenter
Copy link

codecov-commenter commented Jul 8, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.63 🎉

Comparison is base (0d1072b) 90.32% compared to head (2d9e0fd) 90.95%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2369      +/-   ##
==========================================
+ Coverage   90.32%   90.95%   +0.63%     
==========================================
  Files         106      106              
  Lines       54965    60536    +5571     
  Branches    54965    60536    +5571     
==========================================
+ Hits        49647    55063    +5416     
- Misses       5318     5473     +155     
Impacted Files Coverage Δ
lightning/src/chain/chainmonitor.rs 94.96% <100.00%> (+0.45%) ⬆️
lightning/src/chain/channelmonitor.rs 94.72% <100.00%> (+<0.01%) ⬆️

... and 10 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
Copy link
Collaborator Author

I think I've addressed all outstanding feeback, lmk if I should squash.

@jkczyz
Copy link
Contributor

jkczyz commented Jul 10, 2023

I think I've addressed all outstanding feeback, lmk if I should squash.

SGTM

We currently assume the owner of `ChannelMonitor`s won't persist
the `ChannelMonitor` while `Event`s are being processed. This is
fine, except (a) its generally hard to do so and (b) the
`ChainMonitor` doesn't even do this.

Thus, in rare cases, a user could begin processing events which
are, generated by connecting a transaction or a new best-block,
take some time to do so, and while doing so process a further chain
event, causing persistece. This could lose the event being
processed alltogether, which could lose the user funds.

This should be very rare, but may have been made slightly more
reachable with (a) the async event processing making it more
common to do networking in event handling, (b) the new future
generation in the `ChainMonitor`, which now wakes the
`background-processor` directly when chain actions happen on the
`ChainMonitor`.
@TheBlueMatt
Copy link
Collaborator Author

Squashed without diff:

$ git diff-tree -U1 2d9e0fdd3 4206e7119
$ 

@wpaulino wpaulino merged commit 4c342bd into lightningdevkit:main Jul 10, 2023
14 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.

None yet

6 participants