I have a sensor application umbrella project which using Circuits_uart to read sensors data and send those data to another elixir project via websocket and save it to postgresql database.
I make a minimum psudo code of my project to see my current issue.
defmodule Sensor.Application do
use Application
@impl true
def start(_type, _args) do
children = [
Sensor.Config,
Sensor.Threshold,
%{
id: Sensor.SocketClient,
start: {Sensor.SocketClient, :start_link, []}
},
Sensor.Command,
Sensor.Dummy,
Sensor.Counter,
Sensor.Detector,
Sensor.Senosr1Fault,
Sensor.Senosr2Fault,
Sensor.Senosr3Fault,
Sensor.Senosr4Fault,
Sensor.Senosr5Fault,
Sensor.Senosr6Fault,
Sensor.Senosr7Fault,
Sensor.Heartbeat,
Sensor.SendDataToBackend
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_all, name: Sensor.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule Sensor.Command do
def get_data(sensor) do
GenServer.call(__MODULE__, {:get_data, sensor})
catch
:exit, _ ->
{:ok, ""}
end
def handle_call({:get_data, sensor}, _from, state) do
Logger.info("GetCurrentStatus: #{inspect(sensor)}")
status = get_status(sensor, state)
{:reply, status, state}
end
end
I did call the genserver call and wait for default timeout about 5000ms to get result otherwise.
I don’t want to throw or get exception that’s why I use catch and return {:ok, “”}. But I can safe exception and starting getting unexpected callback to handle_info with unexpected parameters is sending to any random genserver handle_info callback. This issue is already mention is the Erlang documents but I didn’t found any solution for this.
Erlang document
Note
Before OTP 24, if the caller uses (try…)catch to avoid process exit, and the server happens to just be late with the reply, it may arrive to the process message queue any time later. The calling process must therefore after catching a time-out exit be prepared to receive garbage message(s) on the form {reference(), _} and deal with them appropriately (discard them) so they do not clog the process message queue or gets mistaken for other messages.
Starting with OTP 24, gen_server:call uses process aliases, so late replies will not be received.