ostinelli / syn

A scalable global Process Registry and Process Group manager for Erlang and Elixir.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error on exit in Elixir

ylankgz opened this issue · comments

commented

I'm getting error on exit with reason {:shutdown, :left} inside elixir phoenix. Is it possible to customize the handle_info EXIT in syn_registry.erl to avoid this error?

Can you please expand, as I don't know what you are referring to.

commented

Yes, sure. We have handle_info EXIT:

handle_info({'EXIT', Pid, Reason}, #state{
    registry_process_exit_callback_module = ProcessExitCallbackModule,
    registry_process_exit_callback_function = ProcessExitCallbackFunction
} = State) ->
    %% do not lock backbone
    spawn(fun() ->
        %% check if pid is in table
        {Key, Meta} = case i_find_by_pid(Pid) of
            undefined ->
                %% log
                case Reason of
                    normal -> ok;
                    killed -> ok;
                    _ ->
                        error_logger:error_msg("Received an exit message from an unlinked process ~p with reason: ~p", [Pid, Reason])
                end,

                %% return
                {undefined, undefined};

            Process ->
                %% get process info
                Key0 = Process#syn_registry_table.key,
                Meta0 = Process#syn_registry_table.meta,

                %% log
                case Reason of
                    normal -> ok;
                    killed -> ok;
                    _ ->
                        error_logger:error_msg("Process with key ~p and pid ~p exited with reason: ~p", [Key0, Pid, Reason])
                end,

                %% delete from table
                remove_process_by_key(Key0),

                %% return
                {Key0, Meta0}
        end,

        %% callback
        case ProcessExitCallbackModule of
            undefined -> ok;
            _ -> ProcessExitCallbackModule:ProcessExitCallbackFunction(Key, Pid, Meta, Reason)
        end
    end),

    %% return
    {noreply, State};

In my case the Reason is {shutdown, left} (not normal or killed), so it gives me error_msg. I would like to get rid of this error. Maybe log errors for myself, or change this callback in sys.config?

In Erlang anything besides normal is to be considered a process exit error. In Syn we consider killed as not generating errors too, because processes can get killed in case of conflict resolution. I am actually considering to change this to a syn-specific exit error instead.

If you are exiting a process with a reason other than normal, such as {shutdown, left}, I recommend changing your architecture instead to use the Callback on process exit option in Syn, which will allow you to use custom behaviours, and keep a clean interface for errors.

commented

Yes exactly, I'm using Callback on process exit

-module(syn_callback).
-export([callback_on_process_exit/4]).

callback_on_process_exit(Key, Pid, Meta, Reason) ->
    error_logger:warning_msg(
        "Process with Key ~p, Pid ~p and Meta ~p exited with reason ~p~n",
        [Key, Pid, Meta, Reason]
    ).

And in my sys.config

{syn, [
    {registry_process_exit_callback, [syn_callback, callback_on_process_exit]}
  ]}

But it's still showing me error_msg next to the warning_msg:

[error] Process with key "<0.553.0>" and pid #PID<0.553.0> exited with reason: {:shutdown, :left}
[warn] Process with Key "<0.553.0>", Pid #PID<0.553.0> and Meta %{id: 1, name: "Ulan"} exited with reason {:shutdown, :left}

Is that correct behaviour?

As I said, you need to modify your architecture to not exit your processes with {shutdown, left}. I can't really know why are you doing so in the first place. :)

commented

It's a vendor issue. I solved it with gen_server. BTW, Syn works perfectly in a cluster, thanks you for your efforts!

Ok, closing.