Skip to content

output_pin, led: Avoid scheduling stale requests - #7447

Open
ghzserg wants to merge 1 commit into
Klipper3d:masterfrom
ghzserg:upstream/fix-output-pin-late-requests
Open

ghzserg wants to merge 1 commit into
Klipper3d:masterfrom
ghzserg:upstream/fix-output-pin-late-requests

Conversation

@ghzserg

@ghzserg ghzserg commented Nov 30, 2026

Copy link
Copy Markdown

A host scheduling stall can cause fan, output pin, and PWM LED commands to be sent with timestamps that are too close to or already behind the MCU clock, resulting in a "Timer too close" shutdown.

This issue was observed on real AD5X hardware:
djvalic/kiln-os#40

Clamp the scheduled time to the estimated MCU print time plus the minimum scheduling interval.

This change covers:

  • Queued G-code requests.
  • Asynchronous requests, including temperature-controlled fans.
  • PWM LED updates.

Requests with sufficient scheduling margin, explicit file-output timestamps, and step scheduling remain unchanged.

Based on the original Kiln OS patch by @djvalic, extended in Z-Mod to cover asynchronous requests.

Thanks to @DIEHARDave for reporting the issue and testing on real hardware.

A host stall can leave a pin, fan or PWM LED update too close to
or behind the MCU clock. Clamp the request time before sending it,
including asynchronous fan updates and repeated requests.
Preserve file-output timing and leave step scheduling unchanged.

Based on djvalic's Kiln OS patch for issue Klipper3d#40, extended to cover
GCodeRequestQueue.send_async_request().

djvalic/kiln-os#40
https://github.com/djvalic/kiln-os/blob/5a87445f9436467d54d20724fe367d5cfddde9e0/buildroot/package/klipper/0003-led-output_pin-send-late-after-a-host-stall.patch

Reported and tested on AD5X by DIEHARDave (Kiln OS issue Klipper3d#40).

Co-authored-by: djvalic <djvalic@users.noreply.github.com>
Signed-off-by: Sergei Rozhkov <zmod@zmod.link>
@ghzserg
ghzserg force-pushed the upstream/fix-output-pin-late-requests branch from 738fefb to 5fc7411 Compare November 30, 2026 10:21
@MRX8024

MRX8024 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

As far as I understand, this moves any late request into the future, including requests that have an explicit print_time. That can make commands run out of sync with the rest of the Gcode. I think a command scheduled for a specific time should run at that time, and not be silently postponed because "it’s too late". If the host stalls for 100-200+ ms (according to your attached issue), the fix belongs in whatever is causing those stalls.

-Maksim

@ghzserg

ghzserg commented Nov 30, 2026

Copy link
Copy Markdown
Author

klipper-pr7447-ad5x-fan-diagnostics.zip
I agree that the underlying causes of host stalls should be addressed. However, I consider host latency and the handling of missed scheduling deadlines to be two separate problems.

Klipper already allows some deviation from the requested timestamps:

  • [output_pin.py:32–39](
    next_time = max(rqueue[0][0], self.next_min_flush_time)
    if next_time > must_flush_time:
    return
    # Skip requests that have been overridden with a following request
    pos = 0
    while pos + 1 < len(rqueue) and rqueue[pos + 1][0] <= next_time:
    pos += 1
    req_pt, req_val = rqueue[pos]
    ) — adjusts scheduled timestamps and skips superseded requests.
  • [output_pin.py:68–88](
    def send_async_request(self, value, print_time=None):
    min_sched_time = self.mcu.min_schedule_time()
    if print_time is None:
    systime = self.printer.get_reactor().monotonic()
    print_time = self.mcu.estimated_print_time(systime + min_sched_time)
    while 1:
    next_time = max(print_time, self.next_min_flush_time)
    # Invoke callback for the request
    action, next_min_time = "normal", 0.
    ret = self.callback(next_time, value)
    if ret is not None:
    # Handle special cases
    action, next_min_time = ret
    self.next_min_flush_time = max(self.next_min_flush_time,
    next_min_time)
    if action == "discard":
    break
    if action == "reschedule":
    continue
    self.next_min_flush_time = max(self.next_min_flush_time,
    next_time + min_sched_time)
    ) — adjusts timestamps for asynchronous requests.
  • [fan.py:60–68](
    if (value and self.kick_start_time
    and (not self.last_fan_value or value - self.last_fan_value > .5)):
    # Run fan at full speed for specified kick_start_time
    self.last_req_value = value
    self.last_fan_value = self.max_power
    self.mcu_fan.set_pwm(print_time, self.max_power)
    return "repeat", print_time + self.kick_start_time
    self.last_fan_value = self.last_req_value = value
    self.mcu_fan.set_pwm(print_time, value)
    ) — delays the requested fan speed when a kick-start is required.
  • [led.py:134–145](
    def update_leds(self, led_state, print_time):
    mcu = self.pins[0][1].get_mcu()
    min_sched_time = mcu.min_schedule_time()
    if print_time is None:
    eventtime = self.printer.get_reactor().monotonic()
    print_time = mcu.estimated_print_time(eventtime) + min_sched_time
    print_time = max(print_time, self.last_print_time + min_sched_time)
    color = led_state[0]
    for idx, mcu_pin in self.pins:
    if self.prev_color[idx] != color[idx]:
    mcu_pin.set_pwm(print_time, color[idx])
    self.last_print_time = print_time
    ) — adjusts timestamps to maintain the minimum interval between PWM updates.

These adjustments serve different purposes from the proposed change, but they demonstrate that the original timestamp is not always an absolute execution deadline.

Furthermore, an explicit print_time does not necessarily imply synchronization with motion. For example, [temperature_fan.py:60–63](

speed_time = read_time + self.speed_delay
self.next_speed_time = speed_time + 0.75 * MAX_FAN_TIME
self.last_speed_value = value
self.fan.set_speed(value, speed_time)
) calculates the timestamp from the sensor reading time and passes it explicitly to fan.set_speed().

I have personally encountered multiple "Timer too close" shutdowns on my AD5X during prints with frequent part cooling fan speed changes.

I recovered the original logs and attached an archive containing two actual shutdowns, a separate diagnostic test, and a detailed explanation.

During the investigation, we deliberately executed a sequence of M106 commands followed by M112 to obtain MCU queue dumps and identify the fan's OID. This allowed us to correlate the PWM commands with the requested fan speeds.

In one of the original shutdowns, a fan PWM command was already approximately 25 ms overdue before it was sent to the MCU.

Eliminating every possible source of host latency is difficult, particularly on resource-constrained hardware. Even if motion scheduling remains valid, a late fan update can currently cause an MCU shutdown.

I understand the concern about preserving synchronization for generic output pins. However, for a part cooling fan or an LED, I believe that a slightly delayed update is preferable to an MCU shutdown and the loss of an otherwise valid print.

The proposed change does not directly adjust timestamps with sufficient scheduling margin or modify step scheduling.

Would restricting this behavior to fans and PWM LEDs be acceptable if changing the behavior of generic output pins is considered undesirable?

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