Skip to content

reactor: Remove completion waiter when pause raises - #7445

Open
ghzserg wants to merge 1 commit into
Klipper3d:masterfrom
ghzserg:upstream/fix-reactor-completion-waiter
Open

ghzserg wants to merge 1 commit into
Klipper3d:masterfrom
ghzserg:upstream/fix-reactor-completion-waiter

Conversation

@ghzserg

@ghzserg ghzserg commented Nov 30, 2026

Copy link
Copy Markdown

If reactor.pause() raises an exception in ReactorCompletion.wait(), the waiting greenlet is not removed from the completion's waiter list.

A subsequent call to complete() may therefore attempt to wake a stale waiter.

Use a try/finally block to ensure the waiter is removed even when an exception occurs.

Normal completion and timeout behavior remain unchanged. No configuration or API changes are required.

@MRX8024

MRX8024 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

A ReactorError here means wait() was called where pausing is not allowed, which is already an internal error, followed by a shutdown. Any other exception escaping pause() would mean the reactor itself is failing or being torn down, so a stale waiter left behind no longer matters.

Is there a piece of code in Klipper that actually reaches this state, or a log that shows the problem?

-Maksim

Ensure ReactorCompletion.wait() removes its registered greenlet even
if reactor.pause() raises an exception. Otherwise a subsequent
complete() may attempt to wake a stale waiter.

Signed-off-by: Sergei Rozhkov <zmod@zmod.link>
Co-authored-by: djvalic <djvalic@users.noreply.github.com>
@ghzserg
ghzserg force-pushed the upstream/fix-reactor-completion-waiter branch from f0e6dc2 to 083fad0 Compare November 30, 2026 11:33
@ghzserg

ghzserg commented Nov 30, 2026

Copy link
Copy Markdown
Author

This issue was observed on a real AD5X running Kiln OS after M112 and restart.

SAVE_VARIABLE was called from a klippy:ready handler, where pausing is prohibited. However, Executor.submit() queues the background operation before calling completion.wait():

The subsequent complete() call encountered the stale waiter and raised AttributeError: 'NoneType' object has no attribute 'timer_is_running', exhausting Klipper's recovery attempt.

Original patch and failure description by djvalic.

The fix prevents this secondary exception without suppressing the original ReactorError.

@nefelim4ag

nefelim4ag commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

I guess the right question is, can you provide an example config that will reproduce the issue on the mainline?

Basically:

[printer]
kinematics: none

[save_variables]
...

[delayed_gcode _something]
initial_duration: 1.1
gcode:
  SAVE_VARIABLES ...

Because if there is a valid configuration that can trigger pause protection, it should be fixed.

Otherwise, I've tried to reproduce something similar:

diff --git a/klippy/extras/save_variables.py b/klippy/extras/save_variables.py
index b6d6ac619..b797d260d 100644
--- a/klippy/extras/save_variables.py
+++ b/klippy/extras/save_variables.py
@@ -22,6 +22,9 @@ class SaveVariables:
         gcode = self.printer.lookup_object('gcode')
         gcode.register_command('SAVE_VARIABLE', self.cmd_SAVE_VARIABLE,
                                desc=self.cmd_SAVE_VARIABLE_help)
+        self.printer.register_event_handler("klippy:ready", self._handle_ready)
+    def _handle_ready(self):
+        self.printer.get_reactor().pause(.0)
     def loadVariables(self):
         allvars = {}
         varfile = configparser.ConfigParser()

So, it seems to me that everything behaves as expected; the printer goes to shutdown:

bed_mesh: zero_reference_position is (205.00, 202.50)
Starting heater checks for heater_bed
Starting heater checks for extruder
Unhandled exception during ready callback
Traceback (most recent call last):
  File "/home/user/klipper/klippy/klippy.py", line 165, in _connect
    cb()
    ~~^^
  File "/home/user/klipper/klippy/extras/save_variables.py", line 27, in _handle_ready
    self.printer.get_reactor().pause(.0)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
  File "/home/user/klipper/klippy/reactor.py", line 232, in pause
    self.verify_can_pause()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/user/klipper/klippy/reactor.py", line 269, in verify_can_pause
    raise ReactorError("Internal error - reactor pause disabled")
reactor.ReactorError: Internal error - reactor pause disabled
Transition to shutdown state: Internal error during ready callback: Internal error - reactor pause disabled
Dumping gcode input 0 blocks
Dumping 15 requests for client 548016828048
...
Internal error during ready callback: Internal error - reactor pause disabled
Once the underlying issue is corrected, use the
...

So, even if I understand what the change does, I'm not sure how it should happen.

Regards,
-Timofey

@MRX8024

MRX8024 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

So, even if I understand what the change does, I'm not sure how it should happen.

The difference is that pause() is called directly there, so no completion is involved and no waiter is left behind. It shows up only through completion.wait() with a completion that is completed later, for example by the executor thread.

+        self.printer.register_event_handler("klippy:ready", self._handle_ready)
+    def _handle_ready(self):
+        self.executor.submit(lambda: None)

In the klippy:ready handler, executor.submit() queues the job and calls completion.wait(), which adds the current greenlet to waiting. Then pause() raises ReactorError, and the greenlet stays in waiting. _connect() catches the error and calls invoke_shutdown(), but the reactor keeps running. When the callback finishes, _end_greenlet() sets the greenlet's timer to None. Later the executor thread finishes the job and calls async_complete(). The reactor processes it in _got_pipe_signal(), and complete() calls update_timer() with the None timer of the stale waiter.

Unhandled exception during run
  File "klippy/reactor.py", line 214, in _got_pipe_signal
    func(*args)
  File "klippy/reactor.py", line 33, in complete
    self.reactor.update_timer(wait.timer, self.reactor.NOW)
  File "klippy/reactor.py", line 141, in update_timer
    if timer_handler.timer_is_running:
AttributeError: 'NoneType' object has no attribute 'timer_is_running'

Still, nothing in mainline calls SAVE_VARIABLE from klippy:ready, so this needs a buggy caller first.

-Maksim

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.

3 participants