question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsonObjectDecoder incorrect CorruptedFrameException?

See original GitHub issue

I have come across some strange behaviour while using the JsonObjectDecoder class. It is very intermittent but I managed to reproduce and provide a unit test below to demonstrate the issue. It appears from some initial investigation that the problem lies with the use of different ByteBufs, but I’ll leave that for your analysis. Perhaps I’m not using the class correctly?

The 2 below JSON Strings are the minimal strings I could reproduce the issue with:

{
   "a":{
      "b":{
         "c":{
            "d":"27301",
            "med":"d",
            "path":"27310"
         }
      },
      "status":"OK"
   }
}

and the partial:

{
   "a":{
      "b":{
         "c":{
            "ory":[
               {
                  "competi":[
                     {
                        "event":[
                           {
                              "externalI":{
                                 "external":[
                                    {
                                       "id":"O"
                                    }
                                 ]

Expected behavior

The JSON strings (one complete, one partial) are submitted to the JsonObjectDecoder split across 2 separate writes. I believe it should not throw a corrupt frame exception, it should produce one JSON string as output and remain read for further data, to arrive to complete the second JSON string, etc.

Actual behavior

The code throws a CorruptedFrameException as indicated by the stacktrace below:

io.netty.handler.codec.CorruptedFrameException: invalid JSON received at byte position 82: 5d

	at io.netty.handler.codec.json.JsonObjectDecoder.decode(JsonObjectDecoder.java:163)
	at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:411)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:373)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:351)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:373)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
	at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:274) 

Steps to reproduce

Please use the Unit Test provided below.

Minimal yet complete reproducer code (or URL to code)

@Test
public void testCorruptedFrameException() {
    final String part1 = "{\"a\":{\"b\":{\"c\":{ \"d\":\"27301\", \"med\":\"d\", \"path\":\"27310\"} }, \"status\":\"OK\" } }{\"";
    final String part2 = "a\":{\"b\":{\"c\":{\"ory\":[{\"competi\":[{\"event\":[{" + "\"externalI\":{\"external\":[{\"id\":\"O\"} ]";
    
    EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());

    ch.writeInbound(Unpooled.copiedBuffer(part1, CharsetUtil.UTF_8));
    ch.readInbound();

    ch.writeInbound(Unpooled.copiedBuffer(part2, CharsetUtil.UTF_8));
    ch.readInbound();
}

Netty version

4.1.6.Final

JVM version (e.g. java -version)

java version “1.8.0_40” Java™ SE Runtime Environment (build 1.8.0_40-b27) Java HotSpot™ 64-Bit Server VM (build 25.40-b25, mixed mode)

OS version (e.g. uname -a)

Darwin <hostname> 16.3.0 Darwin Kernel Version 16.3.0: Thu Nov 17 20:23:58 PST 2016; root:xnu-3789.31.2~1/RELEASE_X86_64 x86_64

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
andrewmccallcommented, Jan 16, 2017

I think you’re right - I’ve added this test to my PR because this seems to fix it and is far simpler than the tests I was trying to write.

0reactions
cdongtaocommented, Sep 29, 2022

hello, I still facing the same issue with using latest version io.netty:4.1.82: io.netty.handler.codec.CorruptedFrameException: invalid JSON received at byte position 523: 00,.Could you help for this and how can I fix this problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonObjectDecoder xref
CorruptedFrameException ; 25 import io.netty.handler.codec. ... false); 61 } 62 63 public JsonObjectDecoder(boolean streamArrayElements) { 64 this(1024 ...
Read more >
Consume json stream data in Netty/Ratpack - Stack Overflow
The problem is the JSON decoder is the first handler in your pipeline, and it's attempting to decode an HTTP post. If I...
Read more >
Java example source code file (JsonObjectDecoder.java)
CorruptedFrameException ; import io.netty.handler.codec. ... false); } public JsonObjectDecoder(boolean streamArrayElements) { this(1024 * 1024, ...
Read more >
io.netty.handler.codec.CorruptedFrameException Java Exaples
CorruptedFrameException Java Examples ... public void testNonJsonContent2() { EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder()); ch.
Read more >
io.netty.handler.codec.CorruptedFrameException java code ...
throw new CorruptedFrameException("length wider than 32-bit"); ... throw new CorruptedFrameException( "invalid JSON received at byte position " + idx + ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found