Browse Source

Normal exit when tcp_closed (#139)

I met a lot of tcp_closed error message when mysql wait_timeout expires,
I'd rather treat this kind of connection close as normal, not error.
My wait_timeout is 2 hours, when night comes, some connection will stay idle for longger than 2 hours and get closed.
So I got many error alerts......
chenduo 5 years ago
parent
commit
b3ef9bfda9
2 changed files with 4 additions and 15 deletions
  1. 2 2
      src/mysql_conn.erl
  2. 2 13
      test/mysql_tests.erl

+ 2 - 2
src/mysql_conn.erl

@@ -414,7 +414,7 @@ handle_info(ping, #state{socket = Socket, sockmod = SockMod} = State) ->
     setopts(SockMod, Socket, [{active, once}]),
     {noreply, update_state(Ok, State)};
 handle_info({tcp_closed, _Socket}, State) ->
-    stop_server(tcp_closed, State);
+    {stop, normal, State#state{socket = undefined, connection_id = undefined}}; 
 handle_info({tcp_error, _Socket, Reason}, State) ->
     stop_server({tcp_error, Reason}, State);
 handle_info(_Info, State) ->
@@ -422,7 +422,7 @@ handle_info(_Info, State) ->
 
 %% @private
 terminate(Reason, #state{socket = Socket, sockmod = SockMod})
-  when Reason == normal; Reason == shutdown ->
+  when Socket =/= undefined andalso (Reason == normal orelse Reason == shutdown) ->
       %% Send the goodbye message for politeness.
       setopts(SockMod, Socket, [{active, false}]),
       mysql_protocol:quit(SockMod, Socket);

+ 2 - 13
test/mysql_tests.erl

@@ -124,19 +124,13 @@ server_disconnect_test() ->
         %% Make the server close the connection after 1 second of inactivity.
         ok = mysql:query(Pid, <<"SET SESSION wait_timeout = 1">>),
         receive
-            {'EXIT', Pid, tcp_closed} -> ok
+            {'EXIT', Pid, normal} -> ok
         after 2000 ->
             no_exit_message
         end
     end),
     process_flag(trap_exit, false),
-    %% Check that we got the expected errors in the error log.
-    [{error, Msg1}, {error, Msg2}, {error_report, CrashReport}] = LoggedErrors,
-    %% "Connection Id 24 closing with reason: tcp_closed"
-    ?assert(lists:prefix("Connection Id", Msg1)),
-    ExpectedPrefix = io_lib:format("** Generic server ~p terminating", [Pid]),
-    ?assert(lists:prefix(lists:flatten(ExpectedPrefix), Msg2)),
-    ?assertMatch({crash_report, _}, CrashReport).
+    ?assertExit(noproc, mysql:stop(Pid)).
 
 tcp_error_test() ->
     process_flag(trap_exit, true),
@@ -177,12 +171,7 @@ keep_alive_test() ->
          end
      end),
      process_flag(trap_exit, false),
-     %% Check that we got the expected crash report in the error log.
      ?assertMatch({'EXIT', Pid, _Reason}, ExitMessage),
-     [{error, LoggedMsg}, {error_report, LoggedReport}] = LoggedErrors,
-     ExpectedPrefix = io_lib:format("** Generic server ~p terminating", [Pid]),
-     ?assert(lists:prefix(lists:flatten(ExpectedPrefix), LoggedMsg)),
-     ?assertMatch({crash_report, _}, LoggedReport),
      ?assertExit(noproc, mysql:stop(Pid)).
 
 reset_connection_test() ->