Bug: Delays are not properly restored when machine created from snapshot

Open
#5,331 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
backend

Research direction

Start by creating a minimal XState v5 machine with an after delay, persist it during the delay, and restore it from the snapshot. Then inspect the delay scheduling and snapshot restoration paths. Done means the delay resumes after restoration and transitions immediately when its expected deadline has already passed.

Written by the indexing model from the issue text.

Description

bug
XState version

XState version 5

Description

When using delays (after: { 1000: "myTarget" }), if the state machine is persisted during a delay and then restored by re-creating it from the snapshot, then the delay will never trigger.

Seems like the delay information is lost when snapshotting.

I've worked around it by using a fromPromise actor with a promise resolving after a setTimeout. The input of that actor is a deadline computed when it is created (Date.now() + delay in ms).

Expected result

The delay should be resumed and possibly trigger its resolution condition if it is past the expected time it should've completed.

Actual result

The delay never resolves and the state machine is stuck forever.

Reproduction

I didn't prepare any, but should be easy?

Additional context

No response

Dominant language
TypeScript
Stars
30.6k
Forks
1.4k
Avg merge
1d 22h
Merged PRs (30d)
90

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from statelyai/xstate

All issues in statelyai/xstate

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.