From cb93a356a07c9b60c836d3487a768050f6ab84c6 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Mon, 7 Jan 2019 11:40:05 +0800 Subject: [PATCH] Fix crash if peer closed the connection (#2120) Prior to this fix, we'll get the following crash if we connected to another emqx broker but got refused because of wrong username or password. --- src/emqx_client.erl | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/emqx_client.erl b/src/emqx_client.erl index 6fbb29724..015bcc51b 100644 --- a/src/emqx_client.erl +++ b/src/emqx_client.erl @@ -715,7 +715,17 @@ waiting_for_connack(timeout, _Timeout, State) -> end; waiting_for_connack(EventType, EventContent, State) -> - handle_event(EventType, EventContent, waiting_for_connack, State). + case take_call(connect, State) of + {value, #call{from = From}, _State} -> + case handle_event(EventType, EventContent, waiting_for_connack, State) of + {stop, Reason, State} -> + Reply = {error, {Reason, EventContent}}, + {stop_and_reply, Reason, [{reply, From, Reply}]}; + StateCallbackResult -> + StateCallbackResult + end; + false -> {stop, connack_timeout} + end. connected({call, From}, subscriptions, State = #state{subscriptions = Subscriptions}) -> {keep_state, State, [{reply, From, maps:to_list(Subscriptions)}]}; @@ -999,8 +1009,8 @@ handle_event(info, {Closed, _Sock}, _StateName, State) when Closed =:= tcp_closed; Closed =:= ssl_closed -> {stop, {shutdown, Closed}, State}; -handle_event(info, {'EXIT', Owner, Reason}, _, #state{owner = Owner}) -> - {stop, Reason}; +handle_event(info, {'EXIT', Owner, Reason}, _, State = #state{owner = Owner}) -> + {stop, Reason, State}; handle_event(info, {inet_reply, _Sock, ok}, _, State) -> {keep_state, State};