From: Matt Corallo Date: Fri, 7 May 2021 22:17:29 +0000 (+0000) Subject: Do not wait in PersistenceNotifier when the persist flag is set X-Git-Tag: v0.0.98~25^2~2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=fdc11f2c76617bc05d8b2c052186f233a34dc709;p=rust-lightning Do not wait in PersistenceNotifier when the persist flag is set When we had a event which caused us to set the persist flag in a PersistenceNotifier in between wait calls, we will still wait, potentially not persisting a ChannelManager when we should. Worse, for wait_timeout, this caused us to always wait up to the timeout, but then always return true that a persistence is needed. Instead, we simply check the persist flag before waiting, returning immediately if it is set. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 9efb6b83..cf6e8cf3 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -4088,6 +4088,10 @@ impl PersistenceNotifier { loop { let &(ref mtx, ref cvar) = &self.persistence_lock; let mut guard = mtx.lock().unwrap(); + if *guard { + *guard = false; + return; + } guard = cvar.wait(guard).unwrap(); let result = *guard; if result { @@ -4103,6 +4107,10 @@ impl PersistenceNotifier { loop { let &(ref mtx, ref cvar) = &self.persistence_lock; let mut guard = mtx.lock().unwrap(); + if *guard { + *guard = false; + return true; + } guard = cvar.wait_timeout(guard, max_wait).unwrap().0; // Due to spurious wakeups that can happen on `wait_timeout`, here we need to check if the // desired wait time has actually passed, and if not then restart the loop with a reduced wait