Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix halfway-stopping of listeners #348

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 28 additions & 15 deletions src/ranch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,37 @@ start_error(_, Error) -> Error.

-spec stop_listener(ref()) -> ok | {error, not_found}.
stop_listener(Ref) ->
%% The stop procedure must be executed in a separate
%% process to make sure that it won't be interrupted
%% in the middle in case the calling process crashes.
%% We use erpc:call locally so we don't have to
%% implement a custom spawn/call mechanism.
%% We need to provide an integer timeout to erpc:call,
%% otherwise the function will be executed in the calling
%% process. 5 minutes should be enough.
erpc:call(node(), fun() -> stop_listener1(Ref) end, 300000).
essen marked this conversation as resolved.
Show resolved Hide resolved

stop_listener1(Ref) ->
TransportAndOpts = maybe_get_transport_and_opts(Ref),
_ = supervisor:terminate_child(ranch_sup, {ranch_listener_sup, Ref}),
Result = supervisor:delete_child(ranch_sup, {ranch_listener_sup, Ref}),
ok = stop_listener2(TransportAndOpts),
Result.

stop_listener2({Transport, TransOpts}) ->
Transport:cleanup(TransOpts),
ok;
stop_listener2(undefined) ->
ok.

maybe_get_transport_and_opts(Ref) ->
try
[_, Transport0, _, _, _] = ranch_server:get_listener_start_args(Ref),
TransOpts0 = get_transport_options(Ref),
{Transport0, TransOpts0}
of
{Transport, TransOpts} ->
case supervisor:terminate_child(ranch_sup, {ranch_listener_sup, Ref}) of
ok ->
_ = supervisor:delete_child(ranch_sup, {ranch_listener_sup, Ref}),
ranch_server:cleanup_listener_opts(Ref),
Transport:cleanup(TransOpts),
ok;
{error, Reason} ->
{error, Reason}
end
[_, Transport, _, _, _] = ranch_server:get_listener_start_args(Ref),
TransOpts = get_transport_options(Ref),
{Transport, TransOpts}
catch
error:badarg ->
{error, not_found}
undefined
essen marked this conversation as resolved.
Show resolved Hide resolved
end.

-spec suspend_listener(ref()) -> ok | {error, any()}.
Expand Down
Loading