xstate: NodeJs memory leak with interpret (xstate>=4.7.0-rc6)
Description
Occurs when using interpret.
Expected Result
After executing the request, classes using interpret will be removed from the memory by GC.
Actual Result
The memory is allocated, but not freed after the request.
Reproduction
I currently have no idea how to show a minimal working example.
Simplified application diagram:
function requestHandler(r, h) {
...
new Kernel() {
...
new SomeModule() {
constructor() {
...
this.interpreter = interpret(
Machine(machineConfig)
);
this.interpreter.onTransition(nextState => {
this.updateState(nextState);
});
...
}
...
afterLoad(pageCtx) {
...
this.interpreter.start(this.initialState);
...
}
beforeUnload() {
...
this.interpreter.stop();
...
}
...
@action
private updateState(nextState) {
this.stateValue = nextState.value;
}
...
}
}
...
// render React to string
// serialize App state
return h.response('html page with serialized App state (data + xstate.StateNode)');
}
Additional context
A memory leak is not observed in version 4.7.0-rc5, but appears in 4.7.0-rc6
There are no tags for these versions, so I could not provide a link to the changes.
In tests, I used this version list: https://www.npmjs.com/package/xstate?activeTab=versions
- xstate >= 4.7.0-rc6
- node >= 12.13.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (8 by maintainers)
I’ve recognized the problem successfully - but it’s not as trivial to fix this as I would have hoped. Gonna think about possible solutions in the following days.
@ccontreras this memory leak in
@xstate/reacthas been fixed in https://github.com/davidkpiano/xstate/pull/1239 by starting machines in an effect. It has not been released yet but we plan to release a new RC version this week or so - so please keep an eye for new releases and in the meantime, I’m going to close this issue.Thanks for the report, I think we know where it comes from. Gonna work on a fix later today.
Thank you for sharing the repro case i will take a look at this soon!
A superficial check showed that a memory leak remained. After the release of version 5, I plan to do a detailed study of the problem. If the problem remains, I will make a repository with a minimal case.