Search code examples
concurrencyerlangerlang-otp

What is the correct way of terminating a child process in a supervisor tree


Lets say I have a Superviser tree with a parent, and some children. The children are transient. but used by other processes. the parent is in charge os pointing the users to the correct child.

User  ---lookup(child)-->  Parent
User <----PidOfChild-----  Parent

User  --request(Resouce)--> Child
User <------respond------   Child

lets say the child shuts down between the two sequences, what would be the correct way of shutting down the child inorder to avoid a crash on the User end?

The user could monitor the Child, but would that help really. since the user is doing all of this inside one transaction?


Solution

  • If you are thinking about an alternate approach to avoid having something in the user side you can make parent as a message broker if you are calling parent and immediately calling child.

    User  ---request(Resouce)-->  Parent --request(Resouce)--> Child
    User <---reponse------------  Parent <-response----------- Child
    

    The parent has to cast the request to the child so that it can avoid blocking. This way the parent can also keep a track of the child and respond accordingly to the user. If the child shuts down, it can restart accordingly or it can shutdown the child if there are no active requests. The parent should not have any processing as it might become a bottleneck in this approach.