eddy147

eddy147

Sometimes getting :noproc error

Once in a while I get a :noproc error:

** (stop) exited in: GenServer.call({:via, Registry, {:trip_generation_server, "804194805327673"}}, :send_to_processing, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
    (elixir 1.14.2) lib/gen_server.ex:1027: GenServer.call/3
    (vehicle_processing 0.1.0) lib/vehicle_processing/processor.ex:1243: VehicleProcessing.Processor.run_trip_generation_server/3
    (stdlib 4.2) gen_server.erl:1161: :gen_server.try_terminate/3
    (stdlib 4.2) gen_server.erl:1351: :gen_server.terminate/10
    (stdlib 4.2) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message: :check_end_of_processing
State: %VehicleProcessing.Processor.State{vehicle: nil, unique_identifier: "804194805327673", data_start_at: nil, data_stop_at: nil, last_active_at: ~U[2023-10-30 10:30:47.181818Z], parking_notification_sent: false, driver_id: %{ibutton: nil, log: nil, permission: nil}, gps_based_properties: %{distance: nil, speed: nil, time: nil}, sent_vehicle_start_trigger?: false, at_least_one_non_heartbeat_message?: true, messages: [], timer: #Reference<0.3285835207.3288072193.194993>, geofences: [], previous_trip_point: nil, gps_reference: nil, previous_state_of_charge: nil, notifications_sent: %{battery_level_low: nil, battery_range_low: nil, battery_state_of_charge_low: nil}, gps_send: 1}

I start the TripGenerationServer dynamically:

  defp run_trip_generation_server(state, f, tries) do
    if tries > 0 do
      case Registry.lookup(:trip_generation_server, state.unique_identifier) do
        [] ->
          case TripGenerationServerSupervisor.start_child(state.unique_identifier) do
            {:ok, _pid} ->
              f.(state)

            e ->
              Logger.warning(
                "Can not start TripGenerationServer #{state.unique_identifier}: #{inspect(e)}"
              )

              Process.sleep(200)
              run_trip_generation_server(state, f, tries - 1)
          end

        [{_pid, _nil}] ->
          f.(state)
      end
    else
      Logger.error("Can not startTripGenerationServer #{state.unique_identifier}")
    end

    state
  end

And I get the :noproc error when

   [{_pid, _nil}] ->
          f.(state)
defmodule VehicleProcessing.TripGenerationServerSupervisor do
  @moduledoc false

  use DynamicSupervisor
  require Logger
  alias VehicleProcessing.TripGenerationServer

  def start_link(_) do
    Logger.info("start #{__MODULE__}")
    DynamicSupervisor.start_link(__MODULE__, [], name: __MODULE__)
  end

  def start_child(unique_identifier) do
    child_spec = %{
      id: TripGenerationServer,
      start: {TripGenerationServer, :start, [unique_identifier]},
      # the child process is restarted only if it terminates abnormally,
      # i.e., with an exit reason other than :normal, :shutdown, or {:shutdown, term}
      restart: :transient,
      shutdown: :brutal_kill,
      type: :worker,
      modules: [TripGenerationServer]
    }

    DynamicSupervisor.start_child(__MODULE__, child_spec)
  end

  @impl true
  def init([]) do
    DynamicSupervisor.init(strategy: :one_for_one)
  end
end

How is this possible?
It also happens sometimes in a test, and when I run the test again, the error does not appear; or it appears in another test, totally random.

Marked As Solved

al2o3cr

al2o3cr

One thought: TripGenerationServerSupervisor.start_child will return the child PID as soon as the process is started, but a call using a via tuple won’t succeed until the child process has registered (presumably using code in TripGenerationServer)

The error you’re seeing could happen if the callback passed to run_trip_generation_server “wins the race” and tries to make a call too early.

Also Liked

dimitarvp

dimitarvp

Oh, don’t do that. I am not sure that’s your problem but always do these with handle_continue!

Also a piece from handle_call docs:

Returning {:reply, reply, new_state, {:continue, continue_arg}} is similar to {:reply, reply, new_state} except that handle_continue/2 will be invoked immediately after with continue_arg as the first argument and state as the second one.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement