Browse Source

Unexpected crash report when unable to connect to server or connection closed by server

Alexander Verbitsky 10 years ago
parent
commit
27ac4ac87a
1 changed files with 15 additions and 10 deletions
  1. 15 10
      src/epgsql_sock.erl

+ 15 - 10
src/epgsql_sock.erl

@@ -202,8 +202,8 @@ command({connect, Host, Username, Password, Opts}, State) ->
              State2#state{handler = auth,
              State2#state{handler = auth,
                           async = Async}};
                           async = Async}};
 
 
-        {error, Reason} = Error ->
-            {stop, Reason, finish(State, Error)}
+        {error, _Reason} = Error ->
+            {stop, shutdown, finish(State, Error)}
     end;
     end;
 
 
 command({squery, Sql}, State) ->
 command({squery, Sql}, State) ->
@@ -680,14 +680,19 @@ on_message({?READY_FOR_QUERY, <<Status:8>>}, State) ->
              end,
              end,
     {noreply, State2#state{txstatus = Status}};
     {noreply, State2#state{txstatus = Status}};
 
 
-on_message(Error = {error, _}, State) ->
-    State2 = case command_tag(State) of
-                 C when C == squery; C == equery; C == execute_batch ->
-                     add_result(State, Error, Error);
-                 _ ->
-                     sync_required(finish(State, Error))
-             end,
-    {noreply, State2};
+on_message(Error = {error, Reason}, State) ->
+    case queue:is_empty(State#state.queue) of
+        true ->
+            {stop, {shutdown, Reason}, State};
+        false ->
+            State2 = case command_tag(State) of
+                C when C == squery; C == equery; C == execute_batch ->
+                    add_result(State, Error, Error);
+                _ ->
+                    sync_required(finish(State, Error))
+            end,
+            {noreply, State2}
+    end;
 
 
 %% NoticeResponse
 %% NoticeResponse
 on_message({?NOTICE, Data}, State) ->
 on_message({?NOTICE, Data}, State) ->