Skip to content

Commit

Permalink
Merge pull request elixir-grpc#375 from beligante/gracefully-handle-s…
Browse files Browse the repository at this point in the history
…erver-process-shutdown

[elixir-grpc#184] gracefully handles server process shutdown
  • Loading branch information
sleipnir authored Jul 2, 2024
2 parents fa640ab + 5883c4a commit ca57719
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 50 deletions.
4 changes: 2 additions & 2 deletions lib/grpc/server/adapters/cowboy/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
end
end

def info({:EXIT, pid, :normal}, req, state = %{pid: pid}) do
exit_handler(pid, :normal)
def info({:EXIT, server_rpc_pid, reason}, req, state = %{pid: server_rpc_pid})
when reason in [:normal, :shutdown] do
{:stop, req, state}
end

Expand Down
21 changes: 21 additions & 0 deletions test/grpc/integration/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ defmodule GRPC.Integration.ServerTest do
def get_feature(point, _stream) do
%Routeguide.Feature{location: point, name: "#{point.latitude},#{point.longitude}"}
end

def route_chat(_ex_stream, stream) do
GRPC.Server.send_headers(stream, %{})
Process.exit(self(), :shutdown)
Process.sleep(500)
end
end

defmodule TranscodeErrorServer do
Expand Down Expand Up @@ -320,6 +326,21 @@ defmodule GRPC.Integration.ServerTest do
end)
end

test "gracefully handles server shutdown disconnects" do
logs =
ExUnit.CaptureLog.capture_log(fn ->
run_server(FeatureServer, fn port ->
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
client_stream = Routeguide.RouteGuide.Stub.route_chat(channel)
assert %GRPC.Client.Stream{} = client_stream
{:ok, ex_stream} = GRPC.Stub.recv(client_stream, timeout: :infinity)
assert [{:error, %GRPC.RPCError{status: 13}}] = Enum.into(ex_stream, [])
end)
end)

assert logs == ""
end

describe "http/json transcode" do
test "grpc method can be called using json when http_transcode == true" do
run_server([TranscodeServer], fn port ->
Expand Down
8 changes: 0 additions & 8 deletions test/support/google/api/annotations.pb.ex

This file was deleted.

40 changes: 0 additions & 40 deletions test/support/google/api/http.pb.ex

This file was deleted.

0 comments on commit ca57719

Please sign in to comment.