Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

The correct solution in this case is to handle it on the User's end: there is no failsafe way of "shutting down" the child such that it responds to the user. There will always be a race condition somewhere that will muck it up.

If you are using a gen_server:cast/call, you can provide a timeout for how long to wait before the request fails and catch it appropriately. If using a raw receive block:

receive
    some_response -> {ok, some_response}
after
    5000 -> % do something.
end.

If your request is constructed before child shutdown, you should use receive ... after ... or gen_server:call scheme. If your request is constructed after child shutdown, maybe you can pending request until timeout or child be avalibe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top