Skip to content

Switch PersistenceNotifierGuard persist closure to FnOnce #3835

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

Merged
merged 1 commit into from
Jun 13, 2025

Conversation

wpaulino
Copy link
Contributor

@wpaulino wpaulino commented Jun 9, 2025

This allows us to move owned and mutable values into the closure, with the assumption that the closure can only be invoked once. As a result, we now have to track should_persist as an Option, such that we can take the owned closure and invoke it within the PersistenceNotifierGuard::drop implementation.

Motivated by #3736 (comment).

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Jun 9, 2025

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

Copy link

codecov bot commented Jun 9, 2025

Codecov Report

Attention: Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Project coverage is 89.90%. Comparing base (217a5b0) to head (1e6f0d3).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channelmanager.rs 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3835      +/-   ##
==========================================
- Coverage   89.93%   89.90%   -0.03%     
==========================================
  Files         163      163              
  Lines      131276   131280       +4     
  Branches   131276   131280       +4     
==========================================
- Hits       118062   118028      -34     
- Misses      10529    10556      +27     
- Partials     2685     2696      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tnull tnull self-requested a review June 9, 2025 07:25
@@ -2815,10 +2815,10 @@ enum NotifyOption {
/// We allow callers to either always notify by constructing with `notify_on_drop` or choose to
/// notify or not based on whether relevant changes have been made, providing a closure to
/// `optionally_notify` which returns a `NotifyOption`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Background question: it seems that in some instances, the persist_check isn't just checking, but also making the actual changes to the chan mgr state. Is that fine, or indicative of the PersistenceNotifierGuard not fully matching its desired use cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In almost all cases it is making actual changes to the internal state, that's how we have the context to know whether we should persist or not via the returned NotifyOption

Copy link
Contributor

@joostjager joostjager Jun 11, 2025

Choose a reason for hiding this comment

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

You'd almost think that the guard itself isn't needed then anymore? I don't have the overview of all uses though.

Would an alternative (not in scope of this PR obviously) be to obtain the guard and then also get some kind of function to set the internal state to needs_persist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That function is exactly what should_persist is doing, but it's less error prone to have each function modify the needs_persist_flag as opposed to having it done in one place by the guard.

Copy link
Contributor

Choose a reason for hiding this comment

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

Will take a closer look at current uses of this. Still it feels to me that the general idea of a scope with a guard is to do the manipulation inside that scope, not deeper nested in the should_persist callback.

tnull
tnull previously approved these changes Jun 10, 2025
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM, one question

@wpaulino wpaulino force-pushed the persistence-notifier-fn-once branch from a6a43c2 to bc420a8 Compare June 10, 2025 17:28
@wpaulino wpaulino requested review from joostjager and tnull June 10, 2025 17:29
optout21
optout21 previously approved these changes Jun 11, 2025
Copy link
Contributor

@optout21 optout21 left a comment

Choose a reason for hiding this comment

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

LGTM. Changing to closure to Option wouldn't have been my obvious solution, but it makes sense.

tnull
tnull previously approved these changes Jun 11, 2025
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

This needs a rebase now

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @joostjager! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@wpaulino wpaulino dismissed stale reviews from tnull and optout21 via 1bc48ab June 12, 2025 17:52
@wpaulino wpaulino force-pushed the persistence-notifier-fn-once branch from bc420a8 to 1bc48ab Compare June 12, 2025 17:52
@wpaulino wpaulino requested a review from tnull June 12, 2025 17:53
This allows us to move owned and mutable values into the closure, with
the assumption that the closure can only be invoked once. As a result,
we now have to track `should_persist` as an `Option`, such that we can
`take` the owned closure and invoke it within the
`PersistenceNotifierGuard::drop` implementation.
@wpaulino wpaulino force-pushed the persistence-notifier-fn-once branch from 1bc48ab to 1e6f0d3 Compare June 12, 2025 18:41
@wpaulino wpaulino requested a review from tnull June 12, 2025 18:41
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM

Some(should_persist) => should_persist,
None => {
debug_assert!(false);
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Silent return in release mode - if this would really happen, do you really want to keep running without any indication in logs etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing a logger just for this would be weird. It should be unreachable though, this is also why I preferred just keeping the unwrap.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, that was that discussion. I'd also have preferred unreachable.

@tnull tnull merged commit afe4285 into lightningdevkit:main Jun 13, 2025
27 checks passed
@wpaulino wpaulino deleted the persistence-notifier-fn-once branch June 13, 2025 16:07
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.

5 participants