Repository navigation
fix: report errors raised while an invocation processes its done.invoke - #698
Merged
Merged
Conversation
When an invoke handler finishes while the machine is idle, its own thread or task sends done.invoke and runs the macrostep. An error raised there was swallowed when the transition exited the invoke's state, and otherwise re-sent as error.execution even on StateMachine. done.invoke is now sent outside the handler's error path: the sync engine lets the error propagate out of the handler thread, and the async engine passes it to the event loop's exception handler. Errors raised by on_finalize go through the engine's error handling, so they become error.execution on StateChart and propagate on StateMachine. Refs #692 Signed-off-by: Fernando Macedo <fgmacedo@gmail.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #698 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 53 53
Lines 5897 5897
Branches 940 940
=========================================
Hits 5897 5897
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Oct 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Refs #692
Why
When an invoke handler finishes while the machine is idle, its own thread (sync engine) or task (async engine) sends
done.invoke.<id>and runs that macrostep itself.InvokeManagerwrapped both the handler'srun()and that send in oneexcept Exception, so an error raised while processingdone.invoke:ctx.cancelledwas already set); the machine rolled back to the old configuration with its invocation cancelled;error.executionotherwise, even onStateMachine(catch_errors_as_events=False).This happened on both engines, not only async: the sync engine surfaced the error only when another thread happened to be driving the loop. Found by the PSSM suite work in #697.
What changes
done.invokeis sent in anelse:branch, so only errors from the handler's ownrun()becomeerror.execution(still only while the state is active, so cancellation semantics are unchanged).done.invokepropagates out of the handler thread, tothreading.excepthook._send_done_asyncpasses it to the event loop's exception handler right away. Left as the task's exception, it only surfaced when the task was garbage collected.handle_external_eventrunson_finalizethrough the engine's_handle_error: a finalize error becomes an internalerror.executiononStateChart(after the external event's transition, as SCXML orders it) and propagates onStateMachine. Without this, theelse:change regressed finalize errors onStateChart, which reachederror.executiononly by accident through the old re-send.docs/invoke.md"Error handling": the existing paragraph said handler errors went to the internal queue and only withcatch_errors_as_events=True; it now matches the code. A new paragraph covers errors while processingdone.invoke.Behavior changes worth a release note (3.3.0 notes do not exist yet, so none was written):
StateMachinewhosedone.invokeprocessing raises now reports the error instead of moving througherror_execution. AStateMachinewitherror_executionbut nodone_invoke_<state>transition now reportsTransitionNotAllowedfordone.invoke.CancelledErrorraised during thedone.invokesend now ends the invocation task cancelled instead of being swallowed. No state difference is observable; nothing awaits that task.#697's harness wraps
sendto record this error (seebuild_machineintests/uml/pssm/test_pssm_cases.py). Once both merge, that workaround can be removed; the harness then observes the error throughthreading.excepthookand the loop's exception handler.Evidence
tests/test_invoke.py, on both engines throughsm_runner:TestInvokeCompletionProcessingErrors(StateMachine leaving the state, StateMachine staying in it, StateChart turning it intoerror.execution) andTestInvokeFinalizeErrors(StateChart and StateMachine). 8 of the 10 fail onmain; the StateChart callback pair passes onmainand stays as a guard.timeout 180 uv run pytest -n 4: 2787 passed, 25 skipped, 44 xfailed; branch coverage 100%. W3C SCXML and threading tests unchanged and green. ruff, mypy, pyright clean; pre-commit hooks passed.call_exception_handler) and an inaccurate existing docs paragraph (fixed). Also flagged theStateMachinewithoutdone_invokechange (accepted, listed above).on_finalizeregression onStateChart(fixed and tested) and that the test helper did not restore the previous loop exception handler (fixed). Also noted thectx.sendpath below.passon both engines), no double reporting on the busy path, and that aStateChartcaller of an unrelated event no longer receives a finalize error.Not verified
ctx.sendfrom insiderun()(for exampletimeout(..., on=...)) is processed by the handler's thread or task too. An error there reachesrun()as the caller ofsend; ifrun()lets it escape after the transition left the state, it is still dropped, as anyrun()error after cancellation. This is unchanged frommain; changing it means changing cancellation semantics, which this PR does not do.StateMachine, a finalize error on the busy path still reaches whichever caller is driving the loop, as onmain.threading.excepthookand the loop exception handler is the channel the maintainer wants, rather than delivering the error to a later caller. The latter would be a public API decision.