Skip to content

[core] Add max iteration count for detecting infinite loops#5430

Merged
davidkpiano merged 9 commits intomainfrom
davidkpiano/max-iterations
Apr 20, 2026
Merged

[core] Add max iteration count for detecting infinite loops#5430
davidkpiano merged 9 commits intomainfrom
davidkpiano/max-iterations

Conversation

@davidkpiano
Copy link
Copy Markdown
Member

@davidkpiano davidkpiano commented Dec 11, 2025

Add maxIterations option to configure the maximum number of microsteps allowed before throwing an infinite loop error. The default is Infinity (no limit) to avoid breaking existing machines.
You can configure it when creating a machine:

const machine = createMachine({
  // ... machine config
  options: {
    maxIterations: 1000 // set a limit to enable infinite loop detection
  }
});

- Introduced `MachineOptions` interface to define runtime options, including `maxIterations`.
- Updated `StateMachine` class to accept and initialize options from the configuration.
- Modified `macrostep` function to utilize the `maxIterations` option, allowing for dynamic iteration limits.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Dec 11, 2025

🦋 Changeset detected

Latest commit: 385d3f7

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

This PR includes changesets to release 5 packages
Name Type
xstate Minor
@xstate/react Major
@xstate/solid Major
@xstate/svelte Major
@xstate/vue Major

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

- Changed default value of `maxIterations` in `MachineOptions` to `Infinity` for no limit.
- Enhanced `macrostep` function to support dynamic iteration limits, throwing an error for infinite loops only when a valid limit is set.
- Updated tests to reflect new `maxIterations` behavior.
@davidkpiano davidkpiano marked this pull request as ready for review January 5, 2026 12:21
Comment thread packages/core/test/transient.test.ts
Comment thread packages/core/src/StateMachine.ts Outdated
Comment thread packages/core/src/stateUtils.ts Outdated
@davidkpiano davidkpiano merged commit e543599 into main Apr 20, 2026
1 check passed
@davidkpiano davidkpiano deleted the davidkpiano/max-iterations branch April 20, 2026 14:26
@github-actions github-actions Bot mentioned this pull request Apr 20, 2026
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.

2 participants