Explorar el Código

Fix the crash caused by keepalive:start/3

Feng Lee hace 9 años
padre
commit
90e46325df
Se han modificado 3 ficheros con 12 adiciones y 7 borrados
  1. 2 1
      src/emqttd_client.erl
  2. 3 4
      src/emqttd_keepalive.erl
  3. 7 2
      src/emqttd_ws_client.erl

+ 2 - 1
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).
+

+ 3 - 4
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.
 

+ 7 - 2
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