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

Example claims subscribe listener can be async #5032

Closed
theluk opened this issue Aug 13, 2024 · 3 comments
Closed

Example claims subscribe listener can be async #5032

theluk opened this issue Aug 13, 2024 · 3 comments

Comments

@theluk
Copy link

theluk commented Aug 13, 2024

Hello,

based on this example we decided to go with xstate, but unfortunately it seems to be a false claim.

Even though in this example a listener is async, the subscribe method does not seem to await it.
This results in very weird db behavior, probably dead locks, but we had to do some wacky work around with queues and setTimeouts.

I would suggest to remove the async listener from the example.

@theluk theluk changed the title Doc claims subscribe listener can be async Example claims subscribe listener can be async Aug 13, 2024
@theluk
Copy link
Author

theluk commented Aug 13, 2024

To reproduce, we use postgres with hasura, we have a machine with quite a few states and events. When we start the machine, it basically runs in a single tick. Sometimes there can be errors, and then the machine is stopping, starting a HITL process. We used subscribe to react to the state changes and save them in the db. But because they are obviously async but the subscribe method does not support them, the machine runs, triggers like 10 different state changes, and then the event loop executes those 10 state changes in a potentially weird order and basically at the same time. Because the order of the promises seems undeterministic, the db often crashes, or at least hasura, because certain triggers on the db side expect a certain state transition, that in this case seems to not be guaranteed

@davidkpiano
Copy link
Member

I refactored the example to a task queue, which would be the appropriate way to handle this to avoid race conditions.

@theluk
Copy link
Author

theluk commented Aug 14, 2024

Yes, but I would suggest not to use the actor.getPersistedState() here, as when the task queue executes the task, the state of the actor might be already somewhere else. The closure should consider only the snapshot.

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

No branches or pull requests

2 participants