Skip to content

Commit 08bce61

Browse files
Merge pull request #1902 from rabbitmq/rabbitmq-server-1901
Ensure resources are cleaned up when connection socket is closed before Ranch handshake completes
2 parents b987346 + 85da671 commit 08bce61

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/rabbit_networking.erl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,29 @@ force_connection_event_refresh(Ref) ->
389389
[rabbit_reader:force_event_refresh(C, Ref) || C <- connections()],
390390
ok.
391391

392-
handshake(Ref, ProxyProtocol) ->
393-
case ProxyProtocol of
392+
failed_to_recv_proxy_header(Ref, Error) ->
393+
Msg = case Error of
394+
closed -> "error when receiving proxy header: TCP socket was ~p prematurely";
395+
_Other -> "error when receiving proxy header: ~p"
396+
end,
397+
rabbit_log:error(Msg, [Error]),
398+
% The following call will clean up resources then exit
399+
_ = ranch:handshake(Ref),
400+
exit({shutdown, failed_to_recv_proxy_header}).
401+
402+
handshake(Ref, ProxyProtocolEnabled) ->
403+
case ProxyProtocolEnabled of
394404
true ->
395-
{ok, ProxyInfo} = ranch:recv_proxy_header(Ref, 1000),
396-
{ok, Sock} = ranch:handshake(Ref),
397-
setup_socket(Sock),
398-
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}};
405+
case ranch:recv_proxy_header(Ref, 3000) of
406+
{error, Error} ->
407+
failed_to_recv_proxy_header(Ref, Error);
408+
{error, protocol_error, Error} ->
409+
failed_to_recv_proxy_header(Ref, Error);
410+
{ok, ProxyInfo} ->
411+
{ok, Sock} = ranch:handshake(Ref),
412+
setup_socket(Sock),
413+
{ok, {rabbit_proxy_socket, Sock, ProxyInfo}}
414+
end;
399415
false ->
400416
{ok, Sock} = ranch:handshake(Ref),
401417
setup_socket(Sock),

0 commit comments

Comments
 (0)