From 90e46325df0f05a39ff47c7161b209540bef591e Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Mon, 13 Mar 2017 12:42:02 +0800 Subject: [PATCH] Fix the crash caused by keepalive:start/3 --- src/emqttd_client.erl | 3 ++- src/emqttd_keepalive.erl | 7 +++---- src/emqttd_ws_client.erl | 9 +++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/emqttd_client.erl b/src/emqttd_client.erl index 998c9f7af..d9543180b 100644 --- a/src/emqttd_client.erl +++ b/src/emqttd_client.erl @@ -252,7 +252,7 @@ handle_info({keepalive, start, Interval}, State = #client_state{connection = Con {error, Error} -> {error, Error} end end, - case emqttd_keepalive:start(StatFun, Interval, {keepalive, check}) of + case emqttd_keepalive:start(Statfun, Interval, {keepalive, check}) of {ok, KeepAlive} -> {noreply, State#client_state{keepalive = KeepAlive}, hibernate}; {error, Error} -> @@ -379,3 +379,4 @@ stop(Reason, State) -> gc(State) -> emqttd_gc:maybe_force_gc(#client_state.force_gc_count, State). + diff --git a/src/emqttd_keepalive.erl b/src/emqttd_keepalive.erl index ae8f6ffbe..5d26ea8a6 100644 --- a/src/emqttd_keepalive.erl +++ b/src/emqttd_keepalive.erl @@ -36,8 +36,8 @@ start(StatFun, TimeoutSec, TimeoutMsg) -> case StatFun() of {ok, StatVal} -> {ok, #keepalive{statfun = StatFun, statval = StatVal, - tsec = TimeoutSec, tmsg = TimeoutMsg, - tref = timer(TimeoutSec, TimeoutMsg)}}; + tsec = TimeoutSec, tmsg = TimeoutMsg, + tref = timer(TimeoutSec, TimeoutMsg)}}; {error, Error} -> {error, Error} end. @@ -64,8 +64,7 @@ resume(KeepAlive = #keepalive{tsec = TimeoutSec, tmsg = TimeoutMsg}) -> %% @doc Cancel Keepalive -spec(cancel(keepalive()) -> ok). cancel(#keepalive{tref = TRef}) when is_reference(TRef) -> - erlang:cancel_timer(TRef), - ok; + catch erlang:cancel_timer(TRef), ok; cancel(_) -> ok. diff --git a/src/emqttd_ws_client.erl b/src/emqttd_ws_client.erl index c89fae7e4..cbe6ab51c 100644 --- a/src/emqttd_ws_client.erl +++ b/src/emqttd_ws_client.erl @@ -198,8 +198,13 @@ handle_info({shutdown, conflict, {ClientId, NewPid}}, State) -> handle_info({keepalive, start, Interval}, State = #wsclient_state{connection = Conn}) -> ?WSLOG(debug, "Keepalive at the interval of ~p", [Interval], State), - KeepAlive = emqttd_keepalive:start(stat_fun(Conn), Interval, {keepalive, check}), - {noreply, State#wsclient_state{keepalive = KeepAlive}, hibernate}; + case emqttd_keepalive:start(stat_fun(Conn), Interval, {keepalive, check}) of + {ok, KeepAlive} -> + {noreply, State#wsclient_state{keepalive = KeepAlive}, hibernate}; + {error, Error} -> + ?LOG(warning, "Keepalive error - ~p", [Error], State), + shutdown(Error, State) + end; handle_info({keepalive, check}, State = #wsclient_state{keepalive = KeepAlive}) -> case emqttd_keepalive:check(KeepAlive) of