From f53cedba9f789d26d76c57f783774b49b49ff99c Mon Sep 17 00:00:00 2001 From: Yuuki Takano Date: Mon, 16 Nov 2026 17:19:32 +0900 Subject: [PATCH 1/2] fix(logger): avoid u8 counter overflow in buffered logger (#728) The buffered logger counted printed messages in a `u8` with `n += 1`, which panics on the 256th message in debug builds. The logger task then moved to the panicked scheduler and all console output stopped. - Use `wrapping_add` for the yield counter. - If the buffered logger task panics, fall back to the raw console via the new `awkernel_lib::logger::unset_buf_console()` so later output, including the panic report, stays visible. Closes #728 Co-Authored-By: Claude Opus 5.5 --- .../awkernel_services/src/buffered_logger.rs | 41 ++++++++++++++++++- awkernel_lib/src/logger.rs | 11 +++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/applications/awkernel_services/src/buffered_logger.rs b/applications/awkernel_services/src/buffered_logger.rs index 17a6e5efd..2c96e6af3 100644 --- a/applications/awkernel_services/src/buffered_logger.rs +++ b/applications/awkernel_services/src/buffered_logger.rs @@ -1,6 +1,11 @@ -use alloc::string::String; +use alloc::{boxed::Box, string::String}; use awkernel_async_lib::channel::bounded; use awkernel_lib::sync::{mcs::MCSNode, mutex::Mutex}; +use core::{ + future::Future, + pin::Pin, + task::{Context, Poll}, +}; static BUFFERED_LOGGER: Mutex> = Mutex::new(None); static LOGGER: Logger = Logger; @@ -44,6 +49,38 @@ impl log::Log for Logger { } pub async fn run() { + FallbackOnPanic(Box::pin(run_logger())).await +} + +/// Switches the logger back to the raw console when the wrapped future panics. +/// +/// If the buffered logger task panicked, it would be moved to the panicked +/// scheduler and nobody would drain the buffer, so all log output, including +/// the panic message, would silently disappear. +struct FallbackOnPanic(Pin>); + +impl Future for FallbackOnPanic { + type Output = F::Output; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + // `guard` is dropped only when `poll` unwinds. + let guard = RawConsoleOnUnwind; + let result = self.get_mut().0.as_mut().poll(cx); + core::mem::forget(guard); + result + } +} + +struct RawConsoleOnUnwind; + +impl Drop for RawConsoleOnUnwind { + fn drop(&mut self) { + awkernel_lib::logger::unset_buf_console(); + log::error!("buffered logger panicked; falling back to the raw console"); + } +} + +async fn run_logger() { let (tx, rx) = bounded::new(bounded::Attribute { queue_size: 4096, ..Default::default() @@ -63,7 +100,7 @@ pub async fn run() { let mut n: u8 = 0; while let Ok(msg) = rx.recv().await { awkernel_lib::console::print(&msg); - n += 1; + n = n.wrapping_add(1); if n & 0x0f == 0 { awkernel_async_lib::r#yield().await; diff --git a/awkernel_lib/src/logger.rs b/awkernel_lib/src/logger.rs index dfc1ac34b..32888ee6d 100644 --- a/awkernel_lib/src/logger.rs +++ b/awkernel_lib/src/logger.rs @@ -111,6 +111,17 @@ pub fn set_buf_console(logger: &'static dyn Log) { } } +/// Stop using the buffered console and fall back to the raw console. +/// +/// This is used when the task draining the buffered console can no longer run, +/// e.g., because it panicked, so that later messages remain visible. +pub fn unset_buf_console() { + let ptr = &raw mut LOGGER; + unsafe { + (*ptr).buf_console = None; + } +} + /// Initialize the logger. /// /// # Safety From d31ad37ae0b9fe120e21f9d4a89247194bebb4bd Mon Sep 17 00:00:00 2001 From: Yuuki Takano Date: Tue, 17 Nov 2026 15:12:46 +0900 Subject: [PATCH 2/2] fix(logger): print the panic report when the buffered logger stops The panic handler queues the panic report into the buffered logger's channel before unwinding, and the receiver was dropped before the fallback guard ran, so the report and all queued messages were lost. - Move the guard into the logger body, after the receiver, and drain the queue to the console when it is dropped. - Switch to direct output by clearing BUFFERED_LOGGER under its lock instead of rewriting the unsynchronized `buf_console` pointer, and remove `awkernel_lib::logger::unset_buf_console()`. - Print the fallback notice as a static string, without allocating during unwinding. Co-Authored-By: Claude Fable 5.2 --- .../awkernel_services/src/buffered_logger.rs | 97 +++++++++---------- awkernel_lib/src/logger.rs | 11 --- 2 files changed, 48 insertions(+), 60 deletions(-) diff --git a/applications/awkernel_services/src/buffered_logger.rs b/applications/awkernel_services/src/buffered_logger.rs index 2c96e6af3..8ffad809b 100644 --- a/applications/awkernel_services/src/buffered_logger.rs +++ b/applications/awkernel_services/src/buffered_logger.rs @@ -1,11 +1,6 @@ -use alloc::{boxed::Box, string::String}; +use alloc::string::String; use awkernel_async_lib::channel::bounded; use awkernel_lib::sync::{mcs::MCSNode, mutex::Mutex}; -use core::{ - future::Future, - pin::Pin, - task::{Context, Poll}, -}; static BUFFERED_LOGGER: Mutex> = Mutex::new(None); static LOGGER: Logger = Logger; @@ -19,68 +14,70 @@ struct Logger; impl log::Log for Logger { fn log(&self, record: &log::Record) { - let mut node = MCSNode::new(); - let mut logger = BUFFERED_LOGGER.lock(&mut node); + { + let mut node = MCSNode::new(); + let mut logger = BUFFERED_LOGGER.lock(&mut node); - let Some(logger) = logger.as_mut() else { - return; - }; + if let Some(logger) = logger.as_mut() { + if logger.tx.is_full() { + logger.discarded += 1; + return; + } - if logger.tx.is_full() { - logger.discarded += 1; - return; - } + let msg = awkernel_lib::logger::format_msg(record); - let msg = awkernel_lib::logger::format_msg(record); + if logger.tx.try_send(msg).is_err() { + logger.discarded += 1; + } - if logger.tx.try_send(msg).is_err() { - logger.discarded += 1; + return; + } } + + // Nobody drains the buffer, so print directly. + // The lock is released first, so that a panic while printing + // does not make the panic handler spin on it. + let msg = awkernel_lib::logger::format_msg(record); + awkernel_lib::console::print(&msg); } fn flush(&self) {} fn enabled(&self, _metadata: &log::Metadata) -> bool { - let mut node = MCSNode::new(); - let logger = BUFFERED_LOGGER.lock(&mut node); - - logger.is_some() + true } } -pub async fn run() { - FallbackOnPanic(Box::pin(run_logger())).await -} - -/// Switches the logger back to the raw console when the wrapped future panics. +/// Stops buffering when the logger task ends, e.g., because it panicked. /// -/// If the buffered logger task panicked, it would be moved to the panicked -/// scheduler and nobody would drain the buffer, so all log output, including -/// the panic message, would silently disappear. -struct FallbackOnPanic(Pin>); - -impl Future for FallbackOnPanic { - type Output = F::Output; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - // `guard` is dropped only when `poll` unwinds. - let guard = RawConsoleOnUnwind; - let result = self.get_mut().0.as_mut().poll(cx); - core::mem::forget(guard); - result - } -} - -struct RawConsoleOnUnwind; +/// If the task panicked, it would be moved to the panicked scheduler and +/// nobody would drain the buffer, so all log output, including the panic +/// report, would silently disappear. +/// +/// This must be declared after the receiver so that it is dropped first. +struct FallbackOnDrop<'a>(&'a bounded::Receiver); -impl Drop for RawConsoleOnUnwind { +impl Drop for FallbackOnDrop<'_> { fn drop(&mut self) { - awkernel_lib::logger::unset_buf_console(); - log::error!("buffered logger panicked; falling back to the raw console"); + // After this, `Logger::log` prints directly and nothing is queued. + let buffered_logger = { + let mut node = MCSNode::new(); + let mut logger = BUFFERED_LOGGER.lock(&mut node); + logger.take() + }; + drop(buffered_logger); + + // Print what is still queued, which includes the panic report. + while let Ok(msg) = self.0.try_recv() { + awkernel_lib::console::print(&msg); + } + + // Do not use `log` here, because it allocates during unwinding. + awkernel_lib::console::print("buffered logger stopped; falling back to direct output\r\n"); } } -async fn run_logger() { +pub async fn run() { let (tx, rx) = bounded::new(bounded::Attribute { queue_size: 4096, ..Default::default() @@ -94,6 +91,8 @@ async fn run_logger() { *logger = Some(buffered_logger); } + let _fallback = FallbackOnDrop(&rx); + awkernel_lib::logger::set_buf_console(&LOGGER); log::set_max_level(log::LevelFilter::Debug); diff --git a/awkernel_lib/src/logger.rs b/awkernel_lib/src/logger.rs index 32888ee6d..dfc1ac34b 100644 --- a/awkernel_lib/src/logger.rs +++ b/awkernel_lib/src/logger.rs @@ -111,17 +111,6 @@ pub fn set_buf_console(logger: &'static dyn Log) { } } -/// Stop using the buffered console and fall back to the raw console. -/// -/// This is used when the task draining the buffered console can no longer run, -/// e.g., because it panicked, so that later messages remain visible. -pub fn unset_buf_console() { - let ptr = &raw mut LOGGER; - unsafe { - (*ptr).buf_console = None; - } -} - /// Initialize the logger. /// /// # Safety