]> granicus.if.org Git - ejabberd/commitdiff
Clean up all timers gracefully
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Fri, 6 Jun 2014 18:29:25 +0000 (22:29 +0400)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Fri, 6 Jun 2014 18:29:50 +0000 (22:29 +0400)
src/mod_sip_registrar.erl

index dcc7617542888259a6f5616976fcf8c4ab5f89ae..7233d053b85851706cb83943bfedad558f7c8dfc 100644 (file)
@@ -271,13 +271,7 @@ write_session([#sip_session{us = {U, S} = US}|_] = NewSessions,
            if length(AllSessions) > MaxSessions ->
                    {error, too_many_sessions};
               true ->
-                   lists:foreach(
-                     fun(#sip_session{reg_tref = TRef,
-                                      conn_mref = MRef} = Session) ->
-                             erlang:cancel_timer(TRef),
-                             catch erlang:demonitor(MRef, [flush]),
-                             mnesia:dirty_delete_object(Session)
-                     end, DelSessions),
+                   lists:foreach(fun delete_session/1, DelSessions),
                    lists:foreach(
                      fun(Session) ->
                              NewSession = set_monitor_and_timer(
@@ -308,12 +302,10 @@ delete_session(US, CallID, CSeq) ->
                true ->
                    ContactsWithExpires =
                        lists:map(
-                         fun(#sip_session{contact = Contact,
-                                          reg_tref = TRef}) ->
-                                 erlang:cancel_timer(TRef),
+                         fun(#sip_session{contact = Contact} = Session) ->
+                                 delete_session(Session),
                                  {Contact, 0}
                          end, Sessions),
-                   mnesia:dirty_delete(sip_session, US),
                    {ok, ContactsWithExpires};
                false ->
                    {error, cseq_out_of_order}
@@ -329,7 +321,7 @@ delete_expired_session(US, TRef) ->
              fun(#sip_session{reg_tref = T1,
                               flow_tref = T2} = Session)
                    when T1 == TRef; T2 == TRef ->
-                     mnesia:dirty_delete_object(Session);
+                     delete_session(Session);
                 (_) ->
                      ok
              end, Sessions);
@@ -526,6 +518,14 @@ set_monitor_and_timer(#sip_session{socket = #sip_socket{type = Type,
 set_timer(#sip_session{us = US}, Timeout) ->
     erlang:start_timer(Timeout * 1000, self(), US).
 
+delete_session(#sip_session{reg_tref = RegTRef,
+                           flow_tref = FlowTRef,
+                           conn_mref = MRef} = Session) ->
+    erlang:cancel_timer(RegTRef),
+    catch erlang:cancel_timer(FlowTRef),
+    catch erlang:demonitor(MRef, [flush]),
+    mnesia:dirty_delete_object(Session).
+
 process_ping(SIPSocket) ->
     ErrResponse = if SIPSocket#sip_socket.type == udp -> error;
                     true -> drop