0
votes

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?

3

3 Answers

1
votes

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.

2
votes

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.
0
votes

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.