Skip to content

Commit 4052c86

Browse files
sleipnirAdriano Santospolvalente
authored
Fix/client disconnect (#493)
* added elixir tools to .gitiignore * fix: Connection crash on disconnect * Update grpc_client/lib/grpc/client/connection.ex Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com> * Update grpc_client/lib/grpc/client/connection.ex Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com> * check format --------- Co-authored-by: Adriano Santos <adriano.santos@v3.com.br> Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
1 parent 78dca5b commit 4052c86

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

grpc_client/lib/grpc/client/connection.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,12 @@ defmodule GRPC.Client.Connection do
234234
resp = {:ok, %Channel{channel | adapter_payload: %{conn_pid: nil}}}
235235

236236
if Map.has_key?(state, :real_channels) do
237-
Enum.map(state.real_channels, fn {_key, {:ok, ch}} ->
238-
adapter.disconnect(ch)
237+
Enum.map(state.real_channels, fn
238+
{_key, {:ok, ch}} ->
239+
do_disconnect(adapter, ch)
240+
241+
_ ->
242+
:ok
239243
end)
240244

241245
keys_to_delete = [:real_channels, :virtual_channel]
@@ -303,6 +307,16 @@ defmodule GRPC.Client.Connection do
303307
{:global, {__MODULE__, ref}}
304308
end
305309

310+
defp do_disconnect(adapter, channel) do
311+
adapter.disconnect(channel)
312+
rescue
313+
_ ->
314+
:ok
315+
catch
316+
_type, _value ->
317+
:ok
318+
end
319+
306320
defp build_initial_state(target, opts) do
307321
opts =
308322
Keyword.validate!(opts,

grpc_client/test/grpc/integration/connection_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ defmodule GRPC.Integration.ConnectionTest do
4545
:ok = GRPC.Server.stop(server)
4646
end
4747
end
48+
49+
test "disconnecting a channel works" do
50+
server = FeatureServer
51+
{:ok, _, port} = GRPC.Server.start(server, 0)
52+
{:ok, channel} = GRPC.Stub.connect("localhost:#{port}")
53+
54+
assert {:ok, _} =
55+
Routeguide.RouteGuide.Stub.get_feature(channel, %Routeguide.Point{
56+
latitude: 409_146_138,
57+
longitude: -746_188_906
58+
})
59+
60+
assert {:ok, _} = GRPC.Stub.disconnect(channel)
61+
:ok = GRPC.Server.stop(server)
62+
end
4863
end

grpc_server/lib/grpc/server/adapters/cowboy/handler.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,7 @@ defmodule GRPC.Server.Adapters.Cowboy.Handler do
628628

629629
defp check_sent_resp(req, status \\ 200)
630630

631-
defp check_sent_resp(%{has_sent_resp: _} = req, status) do
632-
req
633-
end
631+
defp check_sent_resp(%{has_sent_resp: _} = req, _status), do: req
634632

635633
defp check_sent_resp(req, status) do
636634
:cowboy_req.stream_reply(status, req)

0 commit comments

Comments
 (0)