After crash of channel send message to client from monitoring process

I have a genserverprocess that monitors channel- and agent processes. After a channel process crashed I would like to send a message to the client. I suppose the socket becomes invalid a short period after the down message is notified in handle_info (code below) of the genserverprocess. At the moment I’m using TaskChannelHelper.handle_fatal_error (see below) to push a message via this socket, but the message is often not received by the client. What would be the best way to get the message to the client? Maybe some commands to suspend and resume bringing down the channelprocess (edit: according to Process.alive? the process is killed in handle_info while the socket connection in some cases still works)? I’ll take a look at the terminate callback for the channelprocess tomorrow.

  def handle_info({:DOWN, monitor_ref, :process, _pid, reason}, {names, refs}) do
    # if the workflow_agent crashed, remove the workflow from the refs map (result is the crashed item)
    # the task will be removed from the client's tasklists, notify client of the unexpected error
    # if the channel crashed notify the client of an unexpected error and block the task
    result = Map.get(refs, monitor_ref)

    cond do
      is_map(result) and reason !== {:shutdown, :closed} -> socket = result
                                                            TaskChannelHelper.handle_fatal_error("Channel crashed unexpectedly for workflow #{socket.assigns.workflow_id}", socket, :no_action, :no_action)
      reason !== {:shutdown, :closed}                    -> Map.delete(refs, monitor_ref)
                                                            handle_downmessage(result, names, refs, reason)
      true                                               -> nil

    {:noreply, {names, refs}}

Problem solved with the terminate callback.

1 Like