Browse Source

Merge pull request #89 from habibutsu/crash_report_fix

Fixed of error about empty queue
David N. Welton 9 years ago
parent
commit
25dd3c076e
3 changed files with 38 additions and 8 deletions
  1. 1 0
      setup_test_db.sh
  2. 13 8
      src/epgsql_sock.erl
  3. 24 0
      test/epgsql_tests.erl

+ 1 - 0
setup_test_db.sh

@@ -12,6 +12,7 @@ initdb --locale en_US.UTF-8 datadir
 cat > datadir/postgresql.conf <<EOF
 cat > datadir/postgresql.conf <<EOF
 ssl = on
 ssl = on
 ssl_ca_file = 'root.crt'
 ssl_ca_file = 'root.crt'
+lc_messages = 'en_US.UTF-8'
 EOF
 EOF
 
 
 cp test_data/epgsql.crt datadir/server.crt
 cp test_data/epgsql.crt datadir/server.crt

+ 13 - 8
src/epgsql_sock.erl

@@ -697,14 +697,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) ->

+ 24 - 0
test/epgsql_tests.erl

@@ -708,6 +708,30 @@ connection_closed_test(Module) ->
     end,
     end,
     flush().
     flush().
 
 
+connection_closed_by_server_test(Module) ->
+    with_connection(Module,
+        fun(C1) ->
+            P = self(),
+            spawn_link(fun() ->
+                process_flag(trap_exit, true),
+                with_connection(Module,
+                    fun(C2) ->
+                        {ok, _, [{Pid}]} = Module:equery(C2, "select pg_backend_pid()"),
+                        % emulate of disconnection
+                        {ok, _, [{true}]} = Module:equery(C1,
+                            "select pg_terminate_backend($1)", [Pid]),
+                        receive
+                            {'EXIT', C2, {shutdown, #error{code = <<"57P01">>}}} ->
+                                P ! ok;
+                            Other ->
+                                ?debugFmt("Unexpected msg: ~p~n", [Other]),
+                                P ! error
+                        end
+                    end)
+            end),
+            receive ok -> ok end
+        end).
+
 active_connection_closed_test(Module) ->
 active_connection_closed_test(Module) ->
     P = self(),
     P = self(),
     F = fun() ->
     F = fun() ->