Skip to content

clocksync: expose transmit_extra as a tunable [danger_options] knob#2

Open
pdscomp wants to merge 3 commits into
mainfrom
feature/transmit-extra-danger-option
Open

clocksync: expose transmit_extra as a tunable [danger_options] knob#2
pdscomp wants to merge 3 commits into
mainfrom
feature/transmit-extra-danger-option

Conversation

@pdscomp
Copy link
Copy Markdown

@pdscomp pdscomp commented Mar 25, 2026

Problem

When the host computer is under CPU or scheduling pressure, the MCU 'X' shutdown: Timer too close error can occur across multiple MCUs simultaneously. This happens because TRANSMIT_EXTRA in clocksync.py — the fixed 1 ms forward-scheduling headroom added to the MCU clock estimate passed to the serialqueue — is too small to absorb the host's scheduling jitter. The MCU then receives a command whose scheduled waketime has already passed, triggering the hard try_shutdown("Timer too close") guard in src/sched.c.

Because the error originates on the klippy/host side (not in MCU firmware), this is safely fixable without reflashing.

Changes

klippy/clocksync.py

  • Increased the TRANSMIT_EXTRA module-level default from 0.001 s → 0.002 s.
  • Added self.transmit_extra instance attribute (initialized from the module default) so each ClockSync / SecondarySync object can be overridden independently.
  • Added set_transmit_extra(value) method so danger_options can push a user-supplied value in at connect time.
  • _handle_clock() now uses self.transmit_extra instead of the bare TRANSMIT_EXTRA constant.
  • dump_debug() now includes transmit_extra in its output to aid diagnostics.

klippy/extras/danger_options.py

  • Added transmit_extra config option (minval=0.0, maxval=0.010, default sourced from clocksync.TRANSMIT_EXTRA).
  • Registers a klippy:mcu_identify event handler that walks all MCU objects and calls set_transmit_extra() on each _clocksync, so a single config line applies to every MCU (primary + secondary/CAN).

Tuning Guide for End Users

Add to printer.cfg:

[danger_options]
transmit_extra: 0.002   # seconds; default
Scenario Recommended value
Dedicated quiet host (CB1/CM4, nothing else running) 0.001
Default / lightly loaded host 0.002
Shared host with background services 0.0030.004
CAN-bus multi-MCU setups with added bus latency 0.0030.005
Severely overloaded or high-latency host 0.0050.010

Values above 0.010 s are capped by maxval and are not recommended — they push commands so far ahead that scheduling latency for reactive events (endstops, probes) could be marginally affected, though step timing accuracy and print quality are not impacted at normal values.

Do not increase transmit_extra as a substitute for fixing the underlying host load issue. If you need values above 0.004 consistently, investigate CPU usage, swap, disk I/O, or thermal throttling on the host.

Safety

The MCU-side Timer too close guard in src/sched.c is not modified. It is a hard safety check that must remain intact. All changes are confined to the host-side Python layer.

The TRANSMIT_EXTRA constant in clocksync.py controls how far ahead of
the estimated MCU clock the host pre-schedules commands. When the host
is under CPU or scheduling pressure (e.g. competing processes, high
swap, real-time preemption latency) the gap between when klippy
calculates a clock value and when the MCU actually receives the command
can exceed this window. The MCU then sees the scheduled waketime land
in the past and fires the hard shutdown:

    MCU 'hotend' shutdown: Timer too close

This commit:
  1. Changes the default TRANSMIT_EXTRA from 0.001 s (1 ms) to
     0.002 s (2 ms), giving the serialqueue twice as much headroom
     to absorb host scheduling jitter.
  2. Exposes the value as `transmit_extra` in [danger_options] so
     operators can tune it without patching Python.

Tuning guidance for end users
------------------------------
Add or adjust in your printer.cfg:

    [danger_options]
    transmit_extra: 0.002   # default; increase if you see 'Timer too close'

Recommended values by scenario:
  - Quiet dedicated host (e.g. CB1/CM4 running only Klipper): 0.001
  - Default / lightly loaded host:                            0.002
  - Shared host with other services or high CPU load:         0.003-0.004
  - CAN-bus multi-MCU setups with added bus latency:          0.003-0.005

Keeping the value below ~0.010 s is advisable; larger values push
commands further into the future relative to the MCU, which can cause
a small increase in scheduling latency for move commands but has no
impact on step timing accuracy or print quality at normal values.

The MCU-side 'Timer too close' guard in src/sched.c is intentionally
not modified — it is a hard safety check and should not be weakened.
Copilot AI review requested due to automatic review settings March 25, 2026 14:40
Copy link
Copy Markdown

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

Exposes the host-side clock sync forward-scheduling headroom (transmit_extra) as a configurable [danger_options] knob to reduce “Timer too close” MCU shutdowns under host scheduling jitter.

Changes:

  • Increase klippy.clocksync.TRANSMIT_EXTRA default and make it instance-tunable via set_transmit_extra().
  • Add [danger_options] transmit_extra config option with bounds and apply it to all MCU clocksync instances at klippy:mcu_identify.
  • Include transmit_extra in clocksync debug output to aid diagnostics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
klippy/clocksync.py Makes transmit headroom configurable per ClockSync instance and uses it in clock estimation + debug output.
klippy/extras/danger_options.py Adds config plumbing to set transmit_extra and propagates it to all MCU _clocksync objects during connect.

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

Comment thread klippy/extras/danger_options.py Outdated
Comment thread klippy/extras/danger_options.py Outdated
pdscomp and others added 2 commits March 25, 2026 10:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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