19

Working on Grpc Bidirectional Streaming, when i try to run grpc, getting below error

Connection Error
io.netty.handler.codec.http2.Http2Exception: HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: at io.netty.handler.codec.http2.Http2Exception.connectionError(Http2Exception.java:82)
    at io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.readClientPrefaceString(Http2ConnectionHandler.java:322)
    at io.netty.handler.codec.http2.Http2ConnectionHandler$PrefaceDecoder.decode(Http2ConnectionHandler.java:263)
    at io.netty.handler.codec.http2.Http2ConnectionHandler.decode(Http2ConnectionHandler.java:445)
    at io.netty.handler.codec.ByteToMessageDecoder.decodeLast(ByteToMessageDecoder.java:382)
    at io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:286)
    at io.netty.handler.codec.http2.Http2ConnectionHandler.channelInactive(Http2ConnectionHandler.java:421)
    at io.grpc.netty.NettyServerHandler.channelInactive(NettyServerHandler.java:227)
    at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelInactiveNow(ChannelHandlerInvokerUtil.java:56)
    at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelInactive(DefaultChannelHandlerInvoker.java:92)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:135)
    at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:928)
    at io.netty.channel.AbstractChannel$AbstractUnsafe$7.run(AbstractChannel.java:674)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:339)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:356)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:742)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

What could be the issue?

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
Gayathri
  • 221
  • 1
  • 3
  • 8

2 Answers2

34

One thing to be aware of there is the difference between the default behavior of NettyServerBuilder and ManagedChannelBuilder w/r/t plaintext.

If you don't enable TLS on the server (eg, by calling sslContext() or useTransportSecurity() on the server builder), then you must call ManagedChannelBuilder.usePlaintext(true) on any channel connecting to the server.

So, in other words, a server is fine defaulting to plaintext, but for channels you need to call usePlaintext() to allow communication with plaintext servers. If not, you'll get this exact HTTP/2 client preface string missing or corrupt exception (although, in my case, with a particular string of bytes following the hexdump).

John Hart
  • 5,718
  • 2
  • 20
  • 12
7

The client and server aren't agreeing. Typically this is because one is plaintext and the other using TLS. But it can also be due to HTTP/1 vs HTTP/2 in certain environments.

The Hex dump for received bytes is empty though, so there's not enough information to diagnose the issue more precisely. I've never seen the bytes be empty when seeing this failure though.

Eric Anderson
  • 24,057
  • 5
  • 55
  • 76
  • I see this is an older answer, but I ran into something similar and am quite stuck. Do you have any ideas? I am following these two answers but no luck: https://stackoverflow.com/questions/70311138/http-2-client-preface-string-missing-or-corrupt-for-c-client-grpc-using-nanopb Is there a way to create a plaintext channel using C (Arduino project)? – gtree Dec 10 '21 at 22:54
  • This clued in on my issue on Postman. I had used a gRPC connection which worked, then copied that connection inside of Postman. Once I did that and I tried hitting a local gRPC connection, it had this issue. I had to create a new request from scratch, then it worked. – But I'm Not A Wrapper Class Mar 01 '23 at 20:40