From 203b2fcc3879b4b8aeeab1c1b720125a635cb07c Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:38:39 +1100 Subject: [PATCH 1/2] tests: wait for the machine to be on and homed, and for the abort's cancel Since linuxcnc.command() stopped paying 20 ms per command (#4833), three tests flake in CI: - kins-limits/box sends mode(MDI) while the joints are still homing: wait_complete() after home(-1) only waits for task to pass the home on. Task refuses MDI and every move after fails. - matrixkins asks to home before the machine is on, and motion refuses. - twp-native reads g68_active right after a refused MDI, before the abort's plane cancel has gone through task's queue. linuxcnc_util gets wait_for_machine_on() and home_all(), and home_joints() now waits for the machine to be on first. twp-native waits until the plane is cancelled, with a deadline, so a cancel that never comes still fails. Tested in the sandbox under 8 busy-loop CPU burners: on master kins-limits/box failed 1 of 10 rounds; with this change 20 of 20 rounds of all three pass. --- lib/python/linuxcnc_util.py | 44 ++++++++++++++++++++++++++++++++ tests/kins-limits/box/test-ui.py | 5 ++-- tests/twp-native/native.py | 15 +++++++++-- tests/twp-native/test-ui.py | 6 ++--- 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/python/linuxcnc_util.py b/lib/python/linuxcnc_util.py index 6ae8ff9e6f5..b6f3a645d33 100644 --- a/lib/python/linuxcnc_util.py +++ b/lib/python/linuxcnc_util.py @@ -102,6 +102,49 @@ def wait_for_home(self, joints, timeout=10.0): raise LinuxCNC_Exception("timeout waiting for homing to complete:\nstatus.homed:" + str(self.status.homed) + "\nstatus.position:" + str(self.status.position)) + def wait_for_machine_on(self, timeout=10.0): + """Returns once the machine is on, raises LinuxCNC_Exception if + it did not turn on before the timeout. state(STATE_ON) is done + once task has passed it on, the machine is on some cycles later, + and motion refuses to home until it is.""" + + start_time = time.time() + while True: + self.status.poll() + if self.status.task_state == linuxcnc.STATE_ON: + return + if (time.time() - start_time) > timeout: + raise LinuxCNC_Exception("timeout waiting for the machine to turn on: task_state %d" % self.status.task_state) + time.sleep(0.01) + + + def home_all(self, timeout=30.0): + """Arguments: + + 'timeout' is a float, the number of seconds to wait for the + machine to turn on and then for the joints to home. + + Homes every joint with home(-1) once the machine is on. A + command is done once task has passed it on: the machine is on + and the joints are homed some cycles later, and task refuses + homing and MDI until they are. + + Returns once every joint is homed, raises LinuxCNC_Exception if + the machine did not turn on or a joint did not home before the + timeout.""" + + self.wait_for_machine_on(timeout) + self.command.home(-1) + start_time = time.time() + while True: + self.status.poll() + if all(self.status.homed[:self.status.joints]): + return + if (time.time() - start_time) > timeout: + raise LinuxCNC_Exception("timeout waiting for homing to complete:\nstatus.homed:" + str(self.status.homed) + "\nstatus.position:" + str(self.status.position)) + time.sleep(0.01) + + def home_joints(self, joints, timeout=10.0): """Arguments: @@ -118,6 +161,7 @@ def home_joints(self, joints, timeout=10.0): Returns once all the joints are homed, raises LinuxCNC_Exception if one did not home before the timeout.""" + self.wait_for_machine_on(timeout) for joint in joints: self.command.home(joint) start_time = time.time() diff --git a/tests/kins-limits/box/test-ui.py b/tests/kins-limits/box/test-ui.py index 6fb0e193b51..b651f75c356 100755 --- a/tests/kins-limits/box/test-ui.py +++ b/tests/kins-limits/box/test-ui.py @@ -2,6 +2,7 @@ # The [AXIS_L] box is the machine frame envelope, the carriage and not the # tool tip: see README. import linuxcnc +import linuxcnc_util import sys import time @@ -84,9 +85,7 @@ def refused(cmd, needle): c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.wait_complete(30) -c.home(-1) -c.wait_complete(60) +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete(30) drain() diff --git a/tests/twp-native/native.py b/tests/twp-native/native.py index b422c8b803a..4ada4fca876 100644 --- a/tests/twp-native/native.py +++ b/tests/twp-native/native.py @@ -3,6 +3,7 @@ import linuxcnc +import linuxcnc_util import hal import sys import os @@ -29,8 +30,7 @@ def error(self, *a): print("oracle:", a) c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) errors = 0 @@ -68,6 +68,17 @@ def done(timeout=60): time.sleep(0.002) error("timed out waiting for task") +# poll status until cond() holds +def until(cond, what, timeout=60): + deadline = time.time() + timeout + while time.time() < deadline: + s.poll() + if cond(): + return True + time.sleep(0.002) + error("timed out waiting for " + what) + return False + # the move has ended when task has nothing left to run and motion is at # rest; in position with an empty queue alone is also true before task has # sent the move on. Status is a copy task writes once a cycle, so it only diff --git a/tests/twp-native/test-ui.py b/tests/twp-native/test-ui.py index 3042b505b09..db60d734457 100755 --- a/tests/twp-native/test-ui.py +++ b/tests/twp-native/test-ui.py @@ -313,11 +313,11 @@ def pose_near(pairs, b_now, c_now, limited): error("%s was accepted while a plane is active" % cmd) else: print("refused as expected:", m[1]) + # the abort's cancel goes through task's queue: status has it some + # cycles after the error + until(lambda: not s.g68_active, "the abort after %s to cancel the plane" % cmd, 30) c.mode(linuxcnc.MODE_MDI) done() - s.poll() - if s.g68_active: - error("the abort after %s left the plane active" % cmd) mdi("G69") # --- stopping a program that has a plane --------------------------------- From 43f8b29cb5fdfe736f6c72cbb53ccb2a23a7db65 Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:38:39 +1100 Subject: [PATCH 2/2] tests: wait for homing in the other tests that home(-1) The tests below send home(-1), wait_complete() and then switch to MDI or move, the same race kins-limits/box hit: wait_complete() returns once task has passed the home on, not once the joints are homed. They use home_all() now. Tests whose configs have no HOME_SEQUENCE are left as they are: there home(-1) homes nothing and MDI does not need it. All touched tests pass three times in a row at runtests -j 8 in the sandbox. --- tests/kins-limits/jog-travel/test-ui.py | 4 ++-- tests/kins-limits/jog/test-ui.py | 4 ++-- tests/kins-limits/swing/test-ui.py | 4 ++-- tests/kins-orient-linear/test-ui.py | 4 ++-- tests/kins-switch/test-ui.py | 4 ++-- tests/kins-switchkins-core/test-ui.py | 4 ++-- tests/kins-tool-offset/test-ui.py | 4 ++-- tests/ptp-machine-frame/test-ui.py | 4 ++-- tests/tool-info/pocket-numbers/shared/abort-ui.py | 4 ++-- tests/tool-info/pocket-numbers/shared/table-ui.py | 4 ++-- tests/tool-info/pocket-numbers/shared/test-ui.py | 4 ++-- tests/twp-table/test-ui.py | 4 ++-- 12 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/kins-limits/jog-travel/test-ui.py b/tests/kins-limits/jog-travel/test-ui.py index 1d186a69eae..77268078e96 100755 --- a/tests/kins-limits/jog-travel/test-ui.py +++ b/tests/kins-limits/jog-travel/test-ui.py @@ -3,6 +3,7 @@ # reaches its soft limit, and a jog away from it is free: see README. import hal import linuxcnc +import linuxcnc_util import os import sys import time @@ -145,8 +146,7 @@ def jog_ended(what): c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) c.wait_complete(30) -c.home(-1) -c.wait_complete(60) +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete(30) drain() diff --git a/tests/kins-limits/jog/test-ui.py b/tests/kins-limits/jog/test-ui.py index e863759c0e8..e6252c07746 100755 --- a/tests/kins-limits/jog/test-ui.py +++ b/tests/kins-limits/jog/test-ui.py @@ -3,6 +3,7 @@ # own limits: see README. import hal import linuxcnc +import linuxcnc_util import os import sys import time @@ -124,8 +125,7 @@ def tip_held(what): c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) c.wait_complete(30) -c.home(-1) -c.wait_complete(60) +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete(30) drain() diff --git a/tests/kins-limits/swing/test-ui.py b/tests/kins-limits/swing/test-ui.py index 3a84f139ab4..45cd730db10 100755 --- a/tests/kins-limits/swing/test-ui.py +++ b/tests/kins-limits/swing/test-ui.py @@ -3,6 +3,7 @@ # its own limits: see README. import hal import linuxcnc +import linuxcnc_util import os import sys import time @@ -70,8 +71,7 @@ def log_samples(): c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) c.wait_complete(30) -c.home(-1) -c.wait_complete(60) +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete(30) drain() diff --git a/tests/kins-orient-linear/test-ui.py b/tests/kins-orient-linear/test-ui.py index 3d72ea20b34..e737ab401fa 100755 --- a/tests/kins-orient-linear/test-ui.py +++ b/tests/kins-orient-linear/test-ui.py @@ -2,6 +2,7 @@ # 5axiskins orients with B and C; [AXIS_B] TYPE = LINEAR: see README. import linuxcnc +import linuxcnc_util import os import sys @@ -11,8 +12,7 @@ c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete() diff --git a/tests/kins-switch/test-ui.py b/tests/kins-switch/test-ui.py index ed23a791a73..d720bd9d616 100755 --- a/tests/kins-switch/test-ui.py +++ b/tests/kins-switch/test-ui.py @@ -4,6 +4,7 @@ # where the interpreter reads far ahead of the machine. import hal import linuxcnc +import linuxcnc_util import os import sys import time @@ -88,8 +89,7 @@ def mdi(cmd): c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) done(30) -c.home(-1) -done(60) +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() # start in identity every run, the abort's G13.1 may or may not have run yet c.mode(linuxcnc.MODE_MDI) done(30) diff --git a/tests/kins-switchkins-core/test-ui.py b/tests/kins-switchkins-core/test-ui.py index 9cd0e513f0e..e06df189adc 100755 --- a/tests/kins-switchkins-core/test-ui.py +++ b/tests/kins-switchkins-core/test-ui.py @@ -3,6 +3,7 @@ # as type 0 and an X offset of 5 as type 1, and switchkins_core does the rest. import hal import linuxcnc +import linuxcnc_util import sys import time @@ -60,8 +61,7 @@ def check(what, got, want): c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) c.wait_complete(30) -c.home(-1) -c.wait_complete(60) +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete(30) drain() diff --git a/tests/kins-tool-offset/test-ui.py b/tests/kins-tool-offset/test-ui.py index dbd174caa66..76dde0f62ad 100755 --- a/tests/kins-tool-offset/test-ui.py +++ b/tests/kins-tool-offset/test-ui.py @@ -8,6 +8,7 @@ # length back out again through motion alone. import linuxcnc +import linuxcnc_util import hal import subprocess import sys @@ -23,8 +24,7 @@ c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) errors = 0 diff --git a/tests/ptp-machine-frame/test-ui.py b/tests/ptp-machine-frame/test-ui.py index dc23853f7a9..bf84fa44c30 100755 --- a/tests/ptp-machine-frame/test-ui.py +++ b/tests/ptp-machine-frame/test-ui.py @@ -3,6 +3,7 @@ # frame: see README. import linuxcnc +import linuxcnc_util import hal import gcode import preview_helpers @@ -22,8 +23,7 @@ c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) errors = 0 diff --git a/tests/tool-info/pocket-numbers/shared/abort-ui.py b/tests/tool-info/pocket-numbers/shared/abort-ui.py index a46c23cc4bf..97bc93fbff1 100755 --- a/tests/tool-info/pocket-numbers/shared/abort-ui.py +++ b/tests/tool-info/pocket-numbers/shared/abort-ui.py @@ -9,6 +9,7 @@ import time import linuxcnc +import linuxcnc_util c = linuxcnc.command() s = linuxcnc.stat() @@ -59,8 +60,7 @@ def report(): print("=== begin") c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete() drain() diff --git a/tests/tool-info/pocket-numbers/shared/table-ui.py b/tests/tool-info/pocket-numbers/shared/table-ui.py index c6e34229896..2f1d1fbd0b0 100755 --- a/tests/tool-info/pocket-numbers/shared/table-ui.py +++ b/tests/tool-info/pocket-numbers/shared/table-ui.py @@ -8,6 +8,7 @@ import time import linuxcnc +import linuxcnc_util c = linuxcnc.command() s = linuxcnc.stat() @@ -53,8 +54,7 @@ def table(): table() c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() print("tool_offset(1, z=7, x=0.25)") c.tool_offset(1, 7.0, 0.25, 0.0, 0.0, 0.0, 0) diff --git a/tests/tool-info/pocket-numbers/shared/test-ui.py b/tests/tool-info/pocket-numbers/shared/test-ui.py index 988392e8e22..6c764ec93b7 100755 --- a/tests/tool-info/pocket-numbers/shared/test-ui.py +++ b/tests/tool-info/pocket-numbers/shared/test-ui.py @@ -12,6 +12,7 @@ import gcode import hal import linuxcnc +import linuxcnc_util from rs274.interpret import StatMixin c = linuxcnc.command() @@ -113,8 +114,7 @@ def preview(): print("=== begin") c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) c.wait_complete() drain() diff --git a/tests/twp-table/test-ui.py b/tests/twp-table/test-ui.py index 43faad4d101..abf8403b307 100755 --- a/tests/twp-table/test-ui.py +++ b/tests/twp-table/test-ui.py @@ -2,6 +2,7 @@ # G53.1 P1 and P2 on the xyzac-trt sim: see README. import linuxcnc +import linuxcnc_util import sys import os import time @@ -16,8 +17,7 @@ c.state(linuxcnc.STATE_ESTOP_RESET) c.state(linuxcnc.STATE_ON) -c.home(-1) -c.wait_complete() +linuxcnc_util.LinuxCNC(command=c, status=s).home_all() c.mode(linuxcnc.MODE_MDI) errors = 0