Elixir-grpc can't receive stream response. Unexpected when waiting for headers-

Hi, I am trying to stream data to Google Cloud Speech-To-Text and receive a response but almost every request ends up with an error

%GRPC.RPCError{status: 2, message: "unexpected when waiting for headers: {:data, <<0, 0, 0, 0, 62, 42, 4, 50, 2, 8, 21, 50, 54, 10, 34, 10, 27, 32, 109, 97, 121, 98, 101, 32, 109, 97, 121, 98, 101, 32, 115, 111, 109, 101, 116, 104, 105, 110, 103, 32, 101, 108, 115, 101, 21, 57, 172, 96, ...>>}"}

or

%GRPC.RPCError{status: 2, message: "unexpected when waiting for headers: {:trailers, [{\"grpc-status\", \"0\"}, {\"content-disposition\", \"attachment\"}, {\"x-goog-ext-27651507-bin\", \"DbOLmjwVzczMPQ==\"}, {\"x-goog-ext-9650773-bin\", \"DUh68T8=\"}]}"}

The flow is more or less

    GRPC.Stub.connect(@service_url,
      cred: cred,
      adapter_opts: adapter_opts,
      adapter: GRPC.Client.Adapters.Gun,
      headers: %{
        "Authorization" => authorization_header(),
        "Content-Type" => "application/grpc"
      }
    )

    GRPC.Stub.send_request(stream, request)
    GRPC.Stub.recv(stream, timeout: timeout)

When I changed the adapter from Gun to Mint, the library crashes

[error] GenServer #PID<0.16840.0> terminating
** (ArgumentError) request with request reference #Reference<0.200553127.2209087489.150902> was not found
    (mint 1.4.2) lib/mint/http2.ex:765: Mint.HTTP2.get_window_size/2
    (grpc 0.6.0) lib/grpc/client/adapters/mint/connection_process/connection_process.ex:347: GRPC.Client.Adapters.Mint.ConnectionProcess.get_window_size/2
    (grpc 0.6.0) lib/grpc/client/adapters/mint/connection_process/connection_process.ex:205: GRPC.Client.Adapters.Mint.ConnectionProcess.handle_continue/2
    (stdlib 5.1.1) gen_server.erl:1067: :gen_server.try_handle_continue/3
    (stdlib 5.1.1) gen_server.erl:977: :gen_server.loop/7
    (stdlib 5.1.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
Last message: {:continue, :process_request_stream_queue}

Okay I found the problem

That’s because of my default handle_info callback which drops messages. Gun by default sends messages to a process that spawns it. Because it’s my gen server who calls grpc functions, it’s also my gen server who spawns gun

if anything from gun comes to my gen server before I call GRPC.Stub.recv (so gun:await under the hood), this message will be dropped by my handle_info

if messages come after I call GRPC.Stub.recv, everything is okay as Stub.recv is blocking and goes through the whole mailbox

Looks like GRPC.Stub.recv can’t be used in GenServers :thinking: