On my printer host (opi 3 lts), with input shaper enabled, during printing a steppersyncmgr_gen_steps() call from motion_queuing._advance_flush_time() regularly takes tens of milliseconds and at peaks exceeds 100 ms. The call is synchronous, so the reactor is blocked for that time. No timers fire, no other greenlet runs, etc..
E.g. from log:
Reactor 22154.778 busy for 0.082 with:
- 'PrinterMotionQueuing._flush_handler (motion_queuing.py:206)'
Extra logging for the call to ensure that it’s specifically the stepgen, rather than flush callbacks.
_advance_flush_time 22154.7 busy all=0.0812 step_gen_t=0.081
In practice this does not turn into TTC or a motion stall, because steps are generated well ahead of the MCU. The toolhead keeps up to BUFFER_TIME_HIGH=1.0 of moves queued ahead of the estimated print time. The motion_queuing flush timer wakes up once the generated steps are only BGFLUSH_SG_LOW_TIME=0.450 ahead and generates up to BGFLUSH_SG_HIGH_TIME=0.700 ahead, so each batch covers about 250 ms of motion. Even a 100 ms batch leaves roughly 350 ms of margin before the MCU would run out of queued steps.
Still, those 100 ms could be spent on other work. The Gcode greenlet could be parsing and planning the next moves (arcs in particular cost more to process than plain G1 moves), sensor batches could be processed and API clients could be served. So this is not a fix for a specific failure on my hardware I can point at. It is an attempt to make the host more productive and the reactor latency more predictable, which should also leave more headroom on slower SBCs.
A proof of concept is at GitHub - MRX8024/klipper at work-nonblock-chelper-20261024 · GitHub . It is Python only and touches no C code. CFFI already releases the GIL around C calls, so the C step generation can run in parallel with the reactor as long as it is called from another thread and nothing else touches the same C state at the same time. The changes are split into small commits in chelper, toolhead.py and motion_queuing.py.
motion_queuing gets a single worker thread. _advance_flush_time() still does all the Python bookkeeping it did before, flush callbacks, the trapq free and history times, and so on. It also publishes the new last_flush_time and last_step_gen_time up front, so any new moves are queued after this flush. Then it hands steppersyncmgr_gen_steps() to the thread and returns. A reactor completion tracks the batch. When the thread finishes, it returns to the reactor through register_async_callback(), where the result is checked and trapq_finalize_moves() runs.
The harder part is everything else that calls into C while a batch is running, trapq_append() from the toolhead, itersolve position queries, stepcompress queries from motion_report and angle, manual_stepper, and so on. Rather than adding locking to around 20 call sites, chelper.get_ffi() now returns a thin FFISyncLib wrapper around the CFFI lib. Before most C calls it invokes a callback registered by motion_queuing, wait_step_gen(), which pauses the calling greenlet on the completion until the active batch finishes. Greenlets never switch in the middle of a C call, so once a greenlet passes this barrier and enters C, no new batch can start until that greenlet yields again, and one barrier at the FFI boundary is enough.
A few functions are excluded from the barrier. serialqueue_* and trdispatch_* have their own locking. *_alloc, *_free and free must stay callable from destructors at arbitrary points. steppersyncmgr_gen_steps is what the worker thread itself calls. trapq_extract_old only reads history that the thread does not modify. The wrapper also raises an error if a synced call comes from a thread other than the reactor thread, which catches accidental misuse early.
Clock calibration in stats() is split in two. mcu.calibrate_clock() still runs on every stats call without waiting for step generation, so e.g. the ‘mcu lost communication’ check is never delayed. The resulting steppersync_set_time() is applied only right before the next step generation batch.
Klippy tests pass both as is and with a local patch that enables the background thread in batch mode, and the generated steps match master in both cases. On the real printer I ran prints and homing with debug logging, together with motan, whose dump timers constantly query C to stress the barrier, and it seems to work as intended. For now I haven’t tested more thoroughly with manual_stepper or something else “exclusive”.
Any feedback on the approach, or on C call paths I may have missed, is welcome. Thanks!