Skip to content

Commit dd6bfc0

Browse files
authored
Merge pull request #280 from thalesmg/20250318-fix-ws-gun-death-race
fix(ws): avoiding hanging if gun connection process dies before upgrade
2 parents 41472e6 + 1b4d2db commit dd6bfc0

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/emqtt_sock.erl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,23 @@ ssl_upgrade(Host, Sock, SslOpts0, Timeout) ->
7676
send(Sock, Data) when is_port(Sock) ->
7777
send_tcp_data(Sock, Data);
7878
send(#ssl_socket{ssl = SslSock}, Data) ->
79-
ssl:send(SslSock, Data);
79+
case ssl:send(SslSock, Data) of
80+
ok ->
81+
ok;
82+
{error, closed}->
83+
%% We attempt to grab an async exception with more information, if
84+
%% available; otherwise, bail out.
85+
receive
86+
{ssl_error, _Sock, DetailedReason} ->
87+
{error, DetailedReason};
88+
{ssl_closed, _Sock} ->
89+
{error, closed}
90+
after 1 ->
91+
{error, closed}
92+
end;
93+
{error, Reason}->
94+
{error, Reason}
95+
end;
8096
send(QuicStream, Data) when is_reference(QuicStream) ->
8197
case quicer:send(QuicStream, Data) of
8298
{ok, _Len} ->

src/emqtt_ws.erl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ connect(Host0, Port, Opts, Timeout) ->
5151
{ok, StreamRef} -> {ok, {ConnPid, StreamRef}};
5252
Error -> Error
5353
end;
54-
Error -> Error
54+
Error ->
55+
Error
5556
end;
56-
Error -> Error
57+
Error ->
58+
Error
5759
end.
5860

5961
opts([], Acc) -> Acc;
@@ -73,6 +75,8 @@ upgrade(ConnPid, Opts, Timeout) ->
7375
{gun_response, ConnPid, _, _, Status, Headers} ->
7476
{error, {ws_upgrade_failed, Status, Headers}};
7577
{gun_error, ConnPid, StreamRef, Reason} ->
78+
{error, {ws_upgrade_failed, Reason}};
79+
{gun_down, ConnPid, _, Reason, _} ->
7680
{error, {ws_upgrade_failed, Reason}}
7781
after Timeout ->
7882
{error, timeout}

0 commit comments

Comments
 (0)