Genserver.call with timeout doesn't end up with :timeout message

The timeout thing is confusing; I can understand the mixup with the other kind of GenServer timeout…

This is an old post about calls timing out GenServer call time-outs

tl;dr maybe you might want is something along the lines of


try do
  {:ok, checksum} <- Checksum.calculate_checksum(String.to_integer(params["timeout"]))
  conn
      |> put_status(200)
      |> put_view(ChecksumView)
      |> render("checksum.json", checksum: checksum, message: "Checksum calculated successfully!")
catch
  :exit, _value ->
    conn
        |> put_status(408)
        |> put_view(ChecksumView)
        |> render("408.json")
end

Bear in mind that the GenServer process and on completion “the reply message will be sent to the client and if unhandled will clutter the [client] mailbox”.

edit missed Genserver.call with timeout doesn't end up with :timeout message - #16 by lud on scanning through the thread - sorry pretty much what I said.

1 Like

Hey, thanks for the detailed message!

How could I handle that late coming message?

If you need to call the calculate_checksum function only from there then you can do directly as @paulanthonywilson showed. If you want to handle the late message use the Task method.

Yes, only form there, so in that case it cannot cause a problem right without the Task?

Yes, I think that you do not need to worry about it in this case - Phoenix request. I’m petty sure that the request handling processes are short-lived.

If it were something more stateful like a LiveView page or a Channel then you’d want a handle_info/2 to handle (ignore) the message from the timed out call.

1 Like

Thank you for the clarification!

1 Like