Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[#184] gracefully handles server process shutdown #375

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, %{})
Copy link

@zhihuizhang17 zhihuizhang17 Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm investigating a Cowboy shutdown issue for my gRPC service running version v0.9.0 in production. My client receives {:error, %GRPC.RPCError{status: 13, message: "status got is 204 instead of 200"}} when Cowboy shuts down.

In my unit test, if I add this line GRPC.Server.send_headers(stream, %{}), my client receives {:error, %GRPC.RPCError{status: 13, message: ":stream_error: {:stream_error, :no_error, :\"Stream reset by server.\"}"}}.

How can I gracefully handle the service's endpoint shutdown from the client side? Should I send headers like retry or goaway to prompt the client to retry?

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.

Loading