Skip to content

queuelogger: Avoid rollover during header emission - #7446

Open
ghzserg wants to merge 1 commit into
Klipper3d:masterfrom
ghzserg:upstream/fix-queuelogger-rollover
Open

ghzserg wants to merge 1 commit into
Klipper3d:masterfrom
ghzserg:upstream/fix-queuelogger-rollover

Conversation

@ghzserg

@ghzserg ghzserg commented Nov 30, 2026

Copy link
Copy Markdown

Writing the rollover header through self.emit() re-enters the rotating handler and performs another rollover check.

If the system clock advances past the next rollover boundary during rotation, this can trigger a nested rollover, create an empty rotated log file, and duplicate the rollover header.

Write the header directly through logging.FileHandler.emit() to avoid re-entering the rotation logic.

Verified on Python 3.13 with a simulated clock jump during rotation. Before the fix, two rollovers occurred and the header was duplicated. After the fix, only one rollover occurred and the header was written once.

The existing protection introduced in #7387 is preserved.

Write the rollover header directly via FileHandler.emit() rather than
re-entering the rotating handler. A wall clock jump during rollover
can otherwise trigger a nested rollover and duplicate the header.

Signed-off-by: Sergei Rozhkov <zmod@zmod.link>
@MRX8024

MRX8024 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

The change looks fine to me. But how would this happen in practice? The clock would need to jump past the next midnight in the short window between computing rolloverAt and writing the header. Did you see this on a real system?

-Maksim

@ghzserg

ghzserg commented Nov 30, 2026

Copy link
Copy Markdown
Author

On printers without an RTC, the system clock starts at Unix time 0. Klipper may already be running when time synchronization occurs, causing the clock to jump forward by decades.

If synchronization happens during log rotation, the clock can easily jump past the next rollover boundary before the header is written. This is a realistic scenario on embedded systems.

@ghzserg

ghzserg commented Nov 30, 2026

Copy link
Copy Markdown
Author

Yes, we encountered infinite log rotation on real printers running Z-Mod.

These embedded systems can boot with the system clock at Unix time 0 and start Klipper before NTP synchronization, resulting in large clock jumps.

You can see this startup sequence here:
https://github.com/ghzserg/z_ad5x/blob/eee5ae0415162d198490600520e5d62335ea8233/.shell/root/start.sh#L284-L285
https://github.com/ghzserg/z_ad5x/blob/eee5ae0415162d198490600520e5d62335ea8233/.shell/root/start.sh#L391-L403

We resolved our log rotation issues by switching from time-based to size-based rotation:
https://github.com/ghzserg/zmod_klipper/blob/main/klippy/queuelogger.py#L22-L26

I no longer have the original diagnostic logs to confirm that the exact timing scenario described in this PR caused the observed issue. However, large clock jumps while Klipper is running are normal on these systems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants