-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed an infinite loop when initially spawned actor responded synchronously to its parent #2943
Conversation
🦋 Changeset detectedLatest commit: e9f3f07 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
CodeSee Review Map:Review in an interactive map View more CodeSee Maps Legend |
context: TContext, | ||
_event: SCXML.Event<TEvent> = initEvent as SCXML.Event<TEvent> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arguments were reordered to make context
required and a required param can't follow an optional one (_event
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, and this was OK to do because resolveTransition
is a private method, so it's not a breaking change
}); | ||
|
||
// https://github.com/statelyai/xstate/issues/2565 | ||
it('should only spawn an initial actor once when it synchronously responds with an event', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the only new test here
…nously to its parent
b1d8e4e
to
e9f3f07
Compare
@@ -1151,9 +1156,9 @@ class StateNode< | |||
|
|||
private resolveTransition( | |||
stateTransition: StateTransition<TContext, TEvent>, | |||
currentState?: State<TContext, TEvent, any, any>, | |||
_event: SCXML.Event<TEvent> = initEvent as SCXML.Event<TEvent>, | |||
context: TContext = this.machine.context |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the only line that has truly changed - the other stuff is just reordering etc
The reason behind the infinite loop was that this code was running within a "service scope" and thus it has been executing the spawnMachine
. The problem was that this this.machine.context
is actually a getter (yet one reason to hate getters for me :P) and it was executed almost always because the default value for the context
parameter had to be assigned here. In the vast majority of use cases, we are only interested in currentState.context
though - and we were chosing that a couple of lines below:
xstate/packages/core/src/StateNode.ts
Line 1174 in 56842cb
const currentContext = currentState ? currentState.context : context; |
but that was only happening after a default value for
context
was already assigned and the contained side-effect logic executed.
So new child actors were constantly created and their initial actions were constantly executed in response to sendParent
that was contained in those initial actions - thus creating an infinite loop and a memory leak.
I've fixed this by getting rid of this default value and by just providing the appropriate context explicitly at 2 call sites of this function
fixes #2565