Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions deps/rabbit/src/rabbit_ra_systems.erl
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an extra gen_server just to crash the Rabbit app seems a bit hacky to me, though it works. I'm sure there must be a better way to crash Rabbit when ra_systems_sup exits.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

-module(rabbit_ra_systems).

-behaviour(gen_server).

-include_lib("kernel/include/logger.hrl").

-include_lib("rabbit_common/include/logging.hrl").
Expand All @@ -20,6 +22,21 @@
ensure_started/0,
ensure_stopped/0]).

-export([start_link/0,
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-export([stop/0]).

-rabbit_boot_step({rabbit_ra_systems_monitor,
[{description, "monitor process to shut down when Ra systems exit"},
{mfa, {rabbit_sup, start_child, [?MODULE]}},
{requires, [kernel_ready]},
{cleanup, {?MODULE, stop, []}}]}).

-type ra_system_name() :: atom().

-define(COORD_WAL_MAX_SIZE_B, 64_000_000).
Expand Down Expand Up @@ -192,3 +209,38 @@ ensure_ra_system_stopped(RaSystem) ->
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
throw(Error)
end.

%% ----------------------------------------------------------------------------

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

stop() ->
case supervisor:terminate_child(rabbit_sup, ?MODULE) of
ok -> ok = supervisor:delete_child(rabbit_sup, ?MODULE);
{error, not_found} -> ok
end.

init(_) ->
process_flag(trap_exit, true),
Pid = whereis(ra_systems_sup),
true = link(Pid),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t you need to unlink in the terminate/2 callback? Otherwise, stopping rabbit will kill ra_systems_sup. I know that rabbit messes with its dependencies so it might not be a problem right now. But if one day, we fix rabbit, it will be one.

{ok, Pid, hibernate}.

handle_call(_Request, _From, State) -> {noreply, State}.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will leave the caller waiting for a reply, until timeout if it set one. I think you can reply with an ok just to avoid that. What do you think?


handle_cast(_Msg, State) -> {noreply, State}.

handle_info({'EXIT', RaSystemsSup, Reason} = E, RaSystemsSup) ->
?LOG_ERROR(
?MODULE_STRING ": Ra system supervisor exited with reason ~tp~n",
[Reason],
#{domain => ?RMQLOG_DOMAIN_GLOBAL}),
exit(E);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about returning {stop, Reason, State}? Would it have a different behaviour compared to exiting?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah actually, Rabbit doesn't exit unless we use exit/1 here. With {stop, Reason, State} Rabbit keeps going. I'm not really sure why. This is the part that seems pretty hacky to me 😅

handle_info(_Info, State) -> {noreply, State}.

terminate(_, RaSystemsSup) ->
true = unlink(RaSystemsSup),
ok.

code_change(_OldVsn, State, _Extra) -> {ok, State}.
Loading