cln-plugin: Notify waiting tasks if the lightningd connection closes

This is usually a signal that lightningd is shutting down, so notify
any instance that is waiting on `plugin.join()`.

Changelog-Fixed: cln-plugin: Fixed an issue where plugins would hang indefinitely despite `lightningd` closing the connection
This commit is contained in:
Christian Decker
2022-06-30 17:24:35 +02:00
committed by Rusty Russell
parent 12275d0bfe
commit 1efa5c37be
2 changed files with 35 additions and 23 deletions

View File

@@ -14,7 +14,7 @@ struct PluginState {
ca_cert: Vec<u8>,
}
#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
debug!("Starting grpc plugin");
let path = Path::new("lightning-rpc");
@@ -58,13 +58,18 @@ async fn main() -> Result<()> {
let bind_addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap();
tokio::spawn(async move {
if let Err(e) = run_interface(bind_addr, state).await {
warn!("Error running the grpc interface: {}", e);
tokio::select! {
_ = plugin.join() => {
// This will likely never be shown, if we got here our
// parent process is exiting and not processing out log
// messages anymore.
debug!("Plugin loop terminated")
}
});
plugin.join().await
e = run_interface(bind_addr, state) => {
warn!("Error running grpc interface: {:?}", e)
}
}
Ok(())
}
async fn run_interface(bind_addr: SocketAddr, state: PluginState) -> Result<()> {