Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

Copy link

Copilot AI commented Nov 7, 2025

OnEntry handlers were not firing when explicitly transitioning from a child state to its parent state, preventing parent states from acting as conditional routers.

Root Cause

The Enter method skipped entry actions when the source state was within the destination's hierarchy, even for explicit transitions to the parent state itself.

Changes

  • states.go: Modified entry condition to fire OnEntry when explicitly transitioning to a state, even from a child

    // Before: always skipped if source was in hierarchy
    if sr.IncludeState(transition.Source) {
        return nil
    }
    
    // After: only skip if not explicitly transitioning to this state
    if sr.IncludeState(transition.Source) && transition.Destination != sr.State {
        return nil
    }
  • statemachine_test.go: Updated test expectations and added regression test for child→parent transitions

  • example_test.go: Updated expected output (Connected OnEntry now fires when returning from OnHold)

Example Use Case

// Parent state routes to children based on condition
sm.Configure(stateWorking).
    OnEntry(func(_ context.Context, _ ...any) error {
        if condition {
            sm.Fire(triggerToWork1)
        } else {
            sm.Fire(triggerToWork2)
        }
        return nil
    })

sm.Configure(stateWork1).
    SubstateOf(stateWorking).
    Permit(triggerCancel, stateWorking)  // OnEntry now fires on this transition

// Transitioning Work1 → Working now correctly triggers parent's OnEntry routing logic

This aligns with UML statechart semantics where explicit state transitions trigger entry actions.

Original prompt

This section details on the original issue you should resolve

<issue_title>OnEntry not firing when entering parent state from child state in Windows environment</issue_title>
<issue_description>Hello,

We recently experienced some inconsistent behaviors between our windows and linux/macOS environments that I wanted to bring to your attention.

Essentially, we have a reusable set of states that we apply to several different workflows. In this reusable set of states, we have a 'working' state that does work on an item. For most of our workflows, this single state is sufficient, but one workflow has two separate paths of the type of work that should be done, depending on the type of item. So in this case, we decided to give the parent working state two substates. We then override the parent to act as a passthrough, adding an OnEntry that checks a condition and then transitions to one child or the other based on that condition.

There is also a cancel trigger that can apply from this working state or its substates, that returns to the main working state. In other workflows this behaves fine (where there are no child states, and working state simply re-enters itself with the next item). In this case, however, when transitioning from a child of the working state to the parent working state, the onEntry does not trigger and you are not correctly transitioned into the appropriate child state.

From our testing, this seems to only affects windows environments. Our mac devs seem to be unable to reproduce this issue. For our part, we've solved our immediate issue by making our two substates into sibling states of the working state, which does correctly trigger the OnEntry in both windows and linux/mac environments

Image</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 7, 2025 19:39
Co-authored-by: qmuntal <3747816+qmuntal@users.noreply.github.com>
Co-authored-by: qmuntal <3747816+qmuntal@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix OnEntry not firing for parent state in Windows Fix OnEntry not firing when transitioning from child to parent state Nov 7, 2025
Copilot AI requested a review from qmuntal November 7, 2025 19:48
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.

OnEntry not firing when entering parent state from child state in Windows environment

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.