Skip to content
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

Merged
merged 1 commit into from
Jan 17, 2022

Conversation

Andarist
Copy link
Member

fixes #2565

@changeset-bot
Copy link

changeset-bot bot commented Jan 17, 2022

🦋 Changeset detected

Latest commit: e9f3f07

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Patch

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

@ghost
Copy link

ghost commented Jan 17, 2022

CodeSee Review Map:

Review these changes using an interactive CodeSee Map

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

Comment on lines +1160 to +1161
context: TContext,
_event: SCXML.Event<TEvent> = initEvent as SCXML.Event<TEvent>
Copy link
Member Author

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)

Copy link
Member Author

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', () => {
Copy link
Member Author

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

@@ -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
Copy link
Member Author

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:

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

@Andarist Andarist merged commit de07a1b into main Jan 17, 2022
@Andarist Andarist deleted the andarist/fix-2565 branch January 17, 2022 16:43
@github-actions github-actions bot mentioned this pull request Jan 17, 2022
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.

A sendParent used in the entry action of the initial state of a lazily spawned machine crashes the process
2 participants