From debb38cb2b98eb3b7508545e33ff77df1b1b5639 Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Sat, 21 Nov 2026 20:38:31 -0800 Subject: [PATCH 1/2] test(server): supervisor serves on a unix socket Regression for #665. GRPC.Server.Supervisor with adapter_opts: [ip: {:local, path}] fails to start the listener with a Protocol.UndefinedError raised from running_info/4. Red on this commit; the next one fixes it. --- .../adapters/cowboy_unix_socket_test.exs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 grpc_server/test/grpc/server/adapters/cowboy_unix_socket_test.exs diff --git a/grpc_server/test/grpc/server/adapters/cowboy_unix_socket_test.exs b/grpc_server/test/grpc/server/adapters/cowboy_unix_socket_test.exs new file mode 100644 index 00000000..2b059204 --- /dev/null +++ b/grpc_server/test/grpc/server/adapters/cowboy_unix_socket_test.exs @@ -0,0 +1,51 @@ +defmodule GRPC.Server.Adapters.CowboyUnixSocketTest do + use ExUnit.Case, async: true + + import ExUnit.CaptureLog + + defmodule Greeter do + use GRPC.Server, service: Helloworld.Greeter.Service + + def say_hello(%{name: name}, _stream), do: %Helloworld.HelloReply{message: "Hello #{name}"} + end + + @reply_timeout_ms 5_000 + + setup do + path = Path.join(System.tmp_dir!(), "grpc_#{System.unique_integer([:positive])}.sock") + on_exit(fn -> File.rm(path) end) + %{path: path} + end + + test "the supervisor serves on a unix socket and logs its path", %{path: path} do + log = + capture_log(fn -> + start_supervised!( + {GRPC.Server.Supervisor, + servers: [Greeter], port: 0, start_server: true, adapter_opts: [ip: {:local, path}]} + ) + end) + + assert log =~ "Running #{inspect(Greeter)} with Cowboy using http on unix:#{path}" + assert say_hello(path, "unix") == %Helloworld.HelloReply{message: "Hello unix"} + end + + defp say_hello(path, name) do + {:ok, conn} = :gun.open_unix(String.to_charlist(path), %{protocols: [:http2]}) + on_exit(fn -> :gun.close(conn) end) + {:ok, :http2} = :gun.await_up(conn, @reply_timeout_ms) + + message = Protobuf.encode(%Helloworld.HelloRequest{name: name}) + headers = [{"content-type", "application/grpc"}, {"te", "trailers"}] + body = <<0, byte_size(message)::32, message::binary>> + ref = :gun.post(conn, "/helloworld.Greeter/SayHello", headers, body) + + {:response, :nofin, 200, _headers} = :gun.await(conn, ref, @reply_timeout_ms) + + {:ok, <<0, size::32, reply::binary-size(size)>>, trailers} = + :gun.await_body(conn, ref, @reply_timeout_ms) + + assert {"grpc-status", "0"} in trailers + Protobuf.decode(reply, Helloworld.HelloReply) + end +end From 2a8c3cfb34f608e967c7477a9658a538a8fef2c4 Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Sat, 21 Nov 2026 20:38:31 -0800 Subject: [PATCH 2/2] fix(server): start on unix sockets instead of crashing in the startup log :ranch.get_addr/1 returns {:local, path} for a unix socket listener. running_info/4 read it as {ip, port}, passed :local to :inet.ntoa/1 and interpolated the {:error, :einval} it got back, which raised inside Cowboy.start_link/5 and stopped the listener from starting. GRPC.Server.start/3 does not log this line and was not affected. A unix socket listener now logs "Running ... with Cowboy using http on unix:". The IP log line is unchanged. --- grpc_server/CHANGELOG.md | 2 ++ grpc_server/lib/grpc/server/adapters/cowboy.ex | 18 ++++++------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/grpc_server/CHANGELOG.md b/grpc_server/CHANGELOG.md index 0149eab9..6bab9a47 100644 --- a/grpc_server/CHANGELOG.md +++ b/grpc_server/CHANGELOG.md @@ -6,6 +6,8 @@ * A server stream no longer hangs when two calls on one connection fill Cowboy's send buffers. If a stream waited for its stream buffer to drain and the connection buffer then filled, the second alarm made the stream forget it was waiting. When the buffers drained it got no ack, and the call never ended, however fast the client read (#652). + * `GRPC.Server.Supervisor` crashed on unix sockets (`adapter_opts: [ip: {:local, path}]`). This is now fixed (#665). + ## v1.1.0 - 2026-11-13 ### Enhancements diff --git a/grpc_server/lib/grpc/server/adapters/cowboy.ex b/grpc_server/lib/grpc/server/adapters/cowboy.ex index df748aff..21758c76 100644 --- a/grpc_server/lib/grpc/server/adapters/cowboy.ex +++ b/grpc_server/lib/grpc/server/adapters/cowboy.ex @@ -405,20 +405,14 @@ defmodule GRPC.Server.Adapters.Cowboy do defp contains?(_, _), do: false defp running_info(scheme, endpoint, servers, ref) do - {addr, port} = :ranch.get_addr(ref) - - addr_str = - case addr do - :undefined -> - raise "undefined address for ranch server" - - addr -> - "#{:inet.ntoa(addr)}:#{port}" - end - - "Running #{servers_name(endpoint, servers)} with Cowboy using #{scheme}://#{addr_str}" + "Running #{servers_name(endpoint, servers)} with Cowboy using " <> + listen_address(scheme, :ranch.get_addr(ref)) end + defp listen_address(scheme, {:local, path}), do: "#{scheme} on unix:#{path}" + defp listen_address(_scheme, {:undefined, _}), do: raise("undefined address for ranch server") + defp listen_address(scheme, {ip, port}), do: "#{scheme}://#{:inet.ntoa(ip)}:#{port}" + # Without an endpoint, `Module.concat(nil, Dispatch)` is one key for all listeners. # Each listener needs its own routes, so the key uses the listener name. defp dispatch_key(nil, servers), do: {__MODULE__, :dispatch, servers_name(nil, servers)}