Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/grpc_reflection/service/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule GrpcReflection.Service.Builder do
symbol: Enum.find(fields, fn f -> f.name == name end).type_name
}
end)
|> Enum.reject(fn %{symbol: s} -> s == nil end)
|> Enum.reject(fn %{symbol: s} -> is_nil(s) or State.has_symbol?(state, s) end)
|> Enum.reduce(state, fn %{mod: mod, symbol: symbol}, state ->
symbol = Util.trim_symbol(symbol)

Expand Down
15 changes: 15 additions & 0 deletions priv/protos/recursive_message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package recursive_message;

service Service {
rpc call (Request) returns (Reply) {}
}

message Request {
Reply reply = 1;
}

message Reply {
Request request = 1;
}
17 changes: 17 additions & 0 deletions test/service/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,21 @@ defmodule GrpcReflection.Service.BuilderTest do
assert {:ok, tree} = Builder.build_reflection_tree([WrappedService])
assert %State{services: [WrappedService]} = tree
end

test "handles a recursive message structure" do
assert {:ok, tree} = Builder.build_reflection_tree([RecursiveMessage.Service.Service])

assert tree.files |> Map.keys() |> Enum.sort() == [
"recursive_message.Reply.proto",
"recursive_message.Request.proto",
Copy link
Contributor

@zhihuizhang17 zhihuizhang17 Oct 4, 2025

Choose a reason for hiding this comment

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

This fix works for the server side but not the client side. This approach still generates separate files (recursive_message.Request.proto, recursive_message.Reply.proto), even though both messages live in the same source proto. Reflection clients (grpcurl, the Go reflection client, etc.) follow the dependency list, so they’ll bounce back and forth between those synthetic files forever and never resolve the request. In practice that means a FileByFilename loop and eventually a stack overflow—the exact failure we’re trying to eliminate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So the clients are relying on protocol not supporting circular dependencies, so they don't cover it. Interesting

"recursive_message.Service.proto"
]

assert tree.symbols |> Map.keys() |> Enum.sort() == [
"recursive_message.Reply",
"recursive_message.Request",
"recursive_message.Service",
"recursive_message.Service.call"
]
end
end
120 changes: 120 additions & 0 deletions test/support/protos/recursive_message.pb.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
defmodule RecursiveMessage.Request do
@moduledoc false

use Protobuf, protoc_gen_elixir_version: "0.14.1", syntax: :proto3

def descriptor do
# credo:disable-for-next-line
%Google.Protobuf.DescriptorProto{
name: "Request",
field: [
%Google.Protobuf.FieldDescriptorProto{
name: "reply",
extendee: nil,
number: 1,
label: :LABEL_OPTIONAL,
type: :TYPE_MESSAGE,
type_name: ".recursive_message.Reply",
default_value: nil,
options: nil,
oneof_index: nil,
json_name: "reply",
proto3_optional: nil,
__unknown_fields__: []
}
],
nested_type: [],
enum_type: [],
extension_range: [],
extension: [],
options: nil,
oneof_decl: [],
reserved_range: [],
reserved_name: [],
__unknown_fields__: []
}
end

field :reply, 1, type: RecursiveMessage.Reply
end

defmodule RecursiveMessage.Reply do
@moduledoc false

use Protobuf, protoc_gen_elixir_version: "0.14.1", syntax: :proto3

def descriptor do
# credo:disable-for-next-line
%Google.Protobuf.DescriptorProto{
name: "Reply",
field: [
%Google.Protobuf.FieldDescriptorProto{
name: "request",
extendee: nil,
number: 1,
label: :LABEL_OPTIONAL,
type: :TYPE_MESSAGE,
type_name: ".recursive_message.Request",
default_value: nil,
options: nil,
oneof_index: nil,
json_name: "request",
proto3_optional: nil,
__unknown_fields__: []
}
],
nested_type: [],
enum_type: [],
extension_range: [],
extension: [],
options: nil,
oneof_decl: [],
reserved_range: [],
reserved_name: [],
__unknown_fields__: []
}
end

field :request, 1, type: RecursiveMessage.Request
end

defmodule RecursiveMessage.Service.Service do
@moduledoc false

use GRPC.Service, name: "recursive_message.Service", protoc_gen_elixir_version: "0.14.1"

def descriptor do
# credo:disable-for-next-line
%Google.Protobuf.ServiceDescriptorProto{
name: "Service",
method: [
%Google.Protobuf.MethodDescriptorProto{
name: "call",
input_type: ".recursive_message.Request",
output_type: ".recursive_message.Reply",
options: %Google.Protobuf.MethodOptions{
deprecated: false,
idempotency_level: :IDEMPOTENCY_UNKNOWN,
features: nil,
uninterpreted_option: [],
__pb_extensions__: %{},
__unknown_fields__: []
},
client_streaming: false,
server_streaming: false,
__unknown_fields__: []
}
],
options: nil,
__unknown_fields__: []
}
end

rpc :call, RecursiveMessage.Request, RecursiveMessage.Reply
end

defmodule RecursiveMessage.Service.Stub do
@moduledoc false

use GRPC.Stub, service: RecursiveMessage.Service.Service
end