How to verify if a ref is alive?

I want to delay the reply from my GenServer’s handle_call, so my handle_call looks like this:

def handle_call(:foo, from, state) do
  {:noreply, [from | state]}
end

The problem is that when the time to reply to the client comes, the client (i.e. the ref from) could have been dead. So how can I check if it is alive?

Even checking beforehand is prone to a race condition where it dies after the check but before the message being sent back, so you can just not check as well, unless there‘s some benefit to the server by not sending the response.

1 Like

I just want to GenServer.reply to the first living ref, no matter if it is the original ref that GenServer.calls the server.

@LostKobrakai is just noting that this is not something that can be guaranteed. If guarantee of reply is required then some other mechanism (a durable message queue for example) might be required.

Thanks for noting that.

This has the same problem as well. You might check what the first living process is and at the time you actually send the message it’s no longer alive and nobody receives the message.

But what exactly is your task and why does it have this a bit strange requirement?