Skip to content

boot: zephyr: Capture lost early and late log events#2637

Merged
de-nordic merged 1 commit intomcu-tools:mainfrom
Tolt-Technologies:fix/log-drain
Feb 27, 2026
Merged

boot: zephyr: Capture lost early and late log events#2637
de-nordic merged 1 commit intomcu-tools:mainfrom
Tolt-Technologies:fix/log-drain

Conversation

@JayToltTech
Copy link
Contributor

@JayToltTech JayToltTech commented Feb 20, 2026

Problem

When using SWO deferred logging, I noticed lost early and late log messages in my swoviewer.

Summary

  • Fix boot_log_sem initial count from 1 to 0 so zephyr_boot_log_stop() actually blocks until the logging thread finishes draining — previously the semaphore was already signaled at init, so k_sem_take returned immediately without waiting
  • Start the deferred logging thread with K_NO_WAIT instead of a polling-interval delay so log messages are processed immediately
  • Wake the logging thread in zephyr_boot_log_stop() via k_wakeup() so it drains the buffer without waiting for the next polling interval

Test plan

  • Verify all deferred log messages appear before MCUboot jumps to app
  • Verify no log messages are lost on fast boots (no swap)

@JayToltTech JayToltTech marked this pull request as ready for review February 20, 2026 22:49
Copilot AI review requested due to automatic review settings February 20, 2026 22:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates MCUboot’s Zephyr port logging behavior to reduce lost deferred logs and to avoid unnecessarily disabling logging when serial recovery is active and the log transport is not UART.

Changes:

  • Start the custom deferred log processing thread immediately (K_NO_WAIT) rather than after the polling interval delay.
  • Wake the log processing thread during shutdown via k_wakeup() to drain logs without waiting for the next sleep interval.
  • Make ZEPHYR_BOOT_LOG_STOP() conditional on CONFIG_LOG_BACKEND_UART (no-op for non-UART backends).
Comments suppressed due to low confidence (1)

boot/zephyr/main.c:486

  • zephyr_boot_log_stop() intends to wait for the logging thread to exit, but boot_log_sem is defined with an initial count of 1, so the first k_sem_take(..., K_FOREVER) will typically return immediately and not wait for boot_log_thread_func() to k_sem_give(). To make this a real join/drain, initialize the semaphore to 0 (or take it before starting the thread) so zephyr_boot_log_stop() blocks until the thread signals completion.
     * This can be reworked using a thread_join() API once a such will be
     * available in zephyr.
     * see https://github.com/zephyrproject-rtos/zephyr/issues/21500
     */
    (void)k_sem_take(&boot_log_sem, K_FOREVER);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@JayToltTech
Copy link
Contributor Author

zephyr_boot_log_stop() intends to wait for the logging thread to exit, but boot_log_sem is defined with an initial count of 1, so the first k_sem_take(..., K_FOREVER) will typically return immediately and not wait for boot_log_thread_func() to k_sem_give(). To make this a real join/drain, initialize the semaphore to 0 (or take it before starting the thread) so zephyr_boot_log_stop() blocks until the thread signals completion.

I updated this based on the Copilot feedback. Can someone double check this to confirm it's corrected behavior? It sounds correct to me...

@JayToltTech JayToltTech changed the title boot: zephyr: drain deferred log buffer and preserve non-UART backends boot: zephyr: drain deferred log buffer Feb 20, 2026
@JayToltTech JayToltTech changed the title boot: zephyr: drain deferred log buffer boot: zephyr: start log immediately & drain deferred log buffer to fix missed log messages Feb 20, 2026
Copy link
Collaborator

@nordicjm nordicjm left a comment

Choose a reason for hiding this comment

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

Fix is ok but title is wrong, change to boot: zephyr: Fix deferred logging before jumping to app or similar

@JayToltTech JayToltTech changed the title boot: zephyr: start log immediately & drain deferred log buffer to fix missed log messages boot: zephyr: Fix deferred logging before jumping to app Feb 23, 2026
@JayToltTech JayToltTech changed the title boot: zephyr: Fix deferred logging before jumping to app boot: zephyr: Capture lost early and late log events with fixes to zephyr_boot_log_start and zephyr_boot_log_stop Feb 23, 2026
@JayToltTech JayToltTech changed the title boot: zephyr: Capture lost early and late log events with fixes to zephyr_boot_log_start and zephyr_boot_log_stop boot: zephyr: Capture lost early and late log events with fixes to zephyr_boot_log_start, zephyr_boot_log_stop, and boot_log_sem Feb 23, 2026
@JayToltTech JayToltTech changed the title boot: zephyr: Capture lost early and late log events with fixes to zephyr_boot_log_start, zephyr_boot_log_stop, and boot_log_sem boot: zephyr: Capture lost early and late log events Feb 23, 2026
@nordicjm
Copy link
Collaborator

by title, I mean git commit title, that needs updating

The deferred logging thread starts after a polling-interval delay and
stops without waking, leaving a window where final log messages are
lost before MCUboot jumps to the application.

Start the logging thread with K_NO_WAIT so messages are processed
immediately. Wake the thread in zephyr_boot_log_stop() so it drains
the buffer without waiting for the next polling interval.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
@de-nordic de-nordic added the area: zephyr Affects the Zephyr port label Feb 26, 2026
@de-nordic de-nordic merged commit 086c0b4 into mcu-tools:main Feb 27, 2026
65 checks passed
@JayToltTech JayToltTech deleted the fix/log-drain branch March 12, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: zephyr Affects the Zephyr port

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants