Skip to content

Ensure resources are cleaned up when connection socket is closed before Ranch handshake completes #1902

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

Merged
merged 2 commits into from
Feb 27, 2019
Merged
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
28 changes: 22 additions & 6 deletions src/rabbit_networking.erl
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,29 @@ force_connection_event_refresh(Ref) ->
[rabbit_reader:force_event_refresh(C, Ref) || C <- connections()],
ok.

handshake(Ref, ProxyProtocol) ->
case ProxyProtocol of
failed_to_recv_proxy_header(Ref, Error) ->
Msg = case Error of
closed -> "error when receiving proxy header: TCP socket was ~p prematurely";
_Other -> "error when receiving proxy header: ~p"
end,
rabbit_log:error(Msg, [Error]),
% The following call will clean up resources then exit
_ = ranch:handshake(Ref),
exit({shutdown, failed_to_recv_proxy_header}).
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@michaelklishin I don't think that this line will be reached in a real {error, closed} situation as ranch:handshake should exit here. But, there's no harm in leaving this exit call in.


handshake(Ref, ProxyProtocolEnabled) ->
case ProxyProtocolEnabled of
true ->
{ok, ProxyInfo} = ranch:recv_proxy_header(Ref, 1000),
{ok, Sock} = ranch:handshake(Ref),
setup_socket(Sock),
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}};
case ranch:recv_proxy_header(Ref, 3000) of
{error, Error} ->
failed_to_recv_proxy_header(Ref, Error);
{error, protocol_error, Error} ->
failed_to_recv_proxy_header(Ref, Error);
{ok, ProxyInfo} ->
{ok, Sock} = ranch:handshake(Ref),
setup_socket(Sock),
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}}
end;
false ->
{ok, Sock} = ranch:handshake(Ref),
setup_socket(Sock),
Expand Down