From 54c6f6b338cf416de25a65fef26affac01a61d2c Mon Sep 17 00:00:00 2001 From: Luca Toniolo <10792599+grandixximo@users.noreply.github.com> Date: Tue, 29 Sep 2026 11:45:51 +1000 Subject: [PATCH] halui: clear the MDI flag in the same pass that restores the mode When a halui MDI command finishes, modify_hal_pins() restores the Task mode that was active before it, then clears halui_sent_mdi. Both steps tested emcStatus->status == DONE separately, but the restore goes through emcCommandSend(), which refreshes emcStatus while it waits for the echo. If Task echoes the restore while still reporting EXEC, the restore fires and the clear is skipped. The stale flag makes the next sendMdiCommand() keep the old halui_old_mode instead of recording the current mode, so halui later restores the wrong mode. In tests/halui/mdi this shows up as "timeout waiting for task mode to get to 3 (it's 2)" (reported by Bertho in #4604) or "get to 2 (it's 1)", depending on which restore the Task stall hits. Take the DONE decision once, before the restore, and use it for both steps. --- src/emc/usr_intf/halui.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/emc/usr_intf/halui.cc b/src/emc/usr_intf/halui.cc index 1d0cb6e2542..c7fd631da99 100644 --- a/src/emc/usr_intf/halui.cc +++ b/src/emc/usr_intf/halui.cc @@ -1932,8 +1932,12 @@ static void modify_hal_pins() hal_set_bool(halui_data->machine_is_on, emcStatus->task.state == EMC_TASK_STATE::ON); hal_set_bool(halui_data->estop_is_activated, emcStatus->task.state == EMC_TASK_STATE::ESTOP); + // Decide once whether an ongoing halui MDI command has finished: the mode + // restore below refreshes emcStatus, and Task may echo it while still EXEC. + bool mdi_finished = halui_sent_mdi && emcStatus->status == RCS_STATUS::DONE; + if (halui_sent_mdi) { // we have an ongoing MDI command - if (emcStatus->status == RCS_STATUS::DONE) { //which seems to have finished + if (mdi_finished) { //which seems to have finished switch (halui_old_mode) { case EMC_TASK_MODE::MANUAL: sendManual();break; case EMC_TASK_MODE::MDI: break; @@ -1958,7 +1962,7 @@ static void modify_hal_pins() if (num_mdi_commands>0){ // we wants initialize program_is_idle and mode_is_mdi before halui_sent_mdi if (halui_sent_mdi) { // we have an ongoing MDI command - if (emcStatus->status == RCS_STATUS::DONE){ //which seems to have finished + if (mdi_finished) { //which seems to have finished halui_sent_mdi = 0; esleep(0.02); //sleep for a while updateStatus();