#Need to manually stop spawned services?

1 messages · Page 1 of 1 (latest)

swift violet
#

I've been wondering if I'm creating memory leaks in my app by not stopping spawned machines. I have a map machine that spawns "pin" machines based on the results of a search. When a new search is performed, I simply replace the pins ref with a mapping of new machines, something like:

spawnPins: assign((_, { data }) => ({ ref: data.map(datum => pinMachine.withContext(datum) })

Does not iterating over the previously spawned machines and stoping them cause issues? The app seems fine so I dunno if I really need to but I'm also not sure of the repercussions of having orphaned machines (or if I'm even creating them by doing this).

fossil pelican
#

This would cause a memory leak; keep in mind that all spawned/invoked actors are disposed when you stop the parent machine, if that's any relief

swift violet
#

this is very good to know...because the map component would never unmount with how react native navigation works so the main machine would never be stopped

#

would this be considered a side effect that shouldn't be done in assign?

spawnPins: assign(({ ref }, { data }) => {
  ref.forEach(child => child.stop())
  return {
    ref: data.map(datum => pinMachine.withContext(datum))
  }
}
fossil pelican
#

Yes that works

astral shale
#

would this be considered a side effect that shouldn't be done in assign?

yes, this (unfortunately) shouldnt be done in an assign like this

#

this way you don't cleanup the references to the stopped children in the parent machine

#

atm u need to split this:

spawnPins: [
  pure(({ ref }) => ref.map((r) => stop(r)),
  assign(({ ref }, { data }) => {
    return {
      ref: data.map(datum => spawn(pinMachine.withContext(datum)))
    }
  }
]
#

I strongly recommend using this with predictableActionArguments: true

swift violet
#

if I didn't use predictableActionArguments: true or at least preserveActionOrder: true, I'd actually end up stopping my newly started machines right away, right? Since the assign would happen first?

fossil pelican
#

Right

swift violet
#

so I'm not sure why but the above actually breaks my app. I get an error that I can't send a command to child X...fwiw, the spawned machine ID is the ID of the result in the search results but even if I spin it down and then recreate it (in the event the same ID is present in both results), it should work from what I can tell.

#

like if I perform a search and get ids [1,2,3,4,5] and then I move the map and get ids [3,4,5,6,7], shutting down the first set and building the new shouldn't be an issue, right?

fossil pelican
#

Can you share a code repro?

swift violet
#

might be tricky to put together a repro but I can try. If it were a react native app, would it be possible for you to install the deps and run it?

fossil pelican
#

Hmm not really. Did doing it in assign work (despite the impureness)?

swift violet
#

not sure if it worked given @astral shale's feedback that it won't clean up references in the parent...it didn't result in the same crashes, though

#

hmm...so I'm not getting a crash anymore...but I do get a TS error

#

so either it's working fine or it's just not doing anything because stop is effectively a noop...but I dunno how to find out for sure

#

and yes, I realize that you can see in that screen shot that I'm using pure impurely but let's pretend I'm not 🙂 Those just animate a map to a particular region