cln-plugin: do not panic when the log writer task is gone
What changed, and why it matters
This commit fixes a bug in Core Lightning's Rust plugin library where a plugin could crash with a panic if it tried to log a message after the internal log-writing task had already stopped, such as during shutdown. The fix makes the plugin print the message to stderr instead of crashing. It is a reliability fix rather than an active remote exploit.
Treat as a routine bug-fix patch improving plugin shutdown robustness. No urgent security response is indicated, but downstream plugin authors should update to avoid shutdown-time panics.
Security signals we found
panic on send failure replaced with stderr fallback
log message no longer silently dropped on writer task exit
unit test added for dropped-receiver scenario
Evidence from the diff
In plugins/src/logging.rs, LoggingLayer::on_event previously called self.sender.send(…).unwrap(). If the receiver side of the channel had been dropped (because the async runtime was shutting down or the writer task exited), the send failed and the unwrap panicked in whatever code emitted the log event. The patch replaces the unwrap with error handling: on SendError, it extracts the LogEntry and writes it to stderr. A unit test is added that drops the receiver and verifies an info!() log does not panic.
Changed components
cln-plugin Rust libraryplugins/src/logging.rsLoggingLayer tracing subscriberInspect captured patch +29 / −1
### plugins/src/logging.rs
@@ -132,7 +132,15 @@ mod trace {
}
let level = event.metadata().level().into();
- self.sender.send(LogEntry { level, message }).unwrap();
+ if let Err(err) = self.sender.send(LogEntry { level, message }) {
+ // The writer task is gone, most likely because the
+ // runtime is shutting down. We must not panic, and we
+ // must not silently drop the message, so write it to
+ // stderr: stdout is the JSON-RPC channel to
+ // lightningd.
+ let entry = err.0;
+ eprintln!("{:?}: {}", entry.level, entry.message);
+ }
}
}
@@ -174,4 +182,24 @@ mod trace {
"log.target" | "log.module_path" | "log.file" | "log.line"
)
}
+
+ #[cfg(test)]
+ mod tests {
+ use super::*;
+
+ /// The writer task may be gone by the time an event is
+ /// emitted, e.g., during shutdown. The `send` then fails with
+ /// a `SendError`, but logging must never panic: the entry
+ /// must be written to stderr instead.
+ #[test]
+ fn test_logging_layer_with_dropped_writer() {
+ let (sender, receiver) = mpsc::unbounded_channel::<LogEntry>();
+ drop(receiver); // Simulate the writer task having exited.
+
+ let subscriber = tracing_subscriber::registry().with(LoggingLayer::new(sender));
+ tracing::subscriber::with_default(subscriber, || {
+ tracing::info!("this must not panic");
+ });
+ }
+ }
}Why this scored 43/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.