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.

`PooledByteBuf.readBytes` allocates released buffer instead of a writeable one

See original GitHub issue

Expected behavior

ByteBuf.readBytes should not throw an exception because the buffer allocated within the method is already released.

Actual behavior

readBytes when using a PooledByteBufAllocator sometimes (rarely) throws an exception because the buffer allocated inside the method is already released:

io.netty.util.IllegalReferenceCountException: refCnt: 0
  at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1454)
  at io.netty.buffer.AbstractByteBuf.ensureWritable0(AbstractByteBuf.java:289)
  at io.netty.buffer.AbstractByteBuf.ensureWritable(AbstractByteBuf.java:280)
  at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1103)
  at io.netty.buffer.AbstractByteBuf.readBytes(AbstractByteBuf.java:872)
  at eu.cloudnetservice.driver.network.netty.codec.NettyPacketDecoder.decode(NettyPacketDecoder.java:68)

The method newDirectBuffer in PooledByteBufAllocator seems to have a directArea available and allocates a buffer from it (was able to catch it once using a debugger). The buffer returned from that is already released.

Steps to reproduce

As suggested by @chrisvest : “You can try setting io.netty.allocator.smallCacheSize, io.netty.allocator.normalCacheSize, and io.netty.allocator.maxCachedBufferCapacity to 0.”

Then make sure netty is using the PooledByteBufAllocator (for me directByDefault is true) and just try to read bytes of a spcifc length from an incoming buffer (ByteBuf#readBytes(int): ByteBuf)

Sometimes the method will raise an IllegalReferenceCountException when trying to copy the bytes from one buffer to the other.

Minimal yet complete reproducer code (or URL to code)

int length = in.readInt();
ByteBuf body = in.readBytes(length);

Netty version

4.1.77.Final

JVM version (e.g. java -version)

openjdk version “17.0.3” 2022-04-19 OpenJDK Runtime Environment (build 17.0.3+7-Debian-1deb11u1) OpenJDK 64-Bit Server VM (build 17.0.3+7-Debian-1deb11u1, mixed mode, sharing)

OS version (e.g. uname -a)

Linux 5.10.0-13-amd64 #1 SMP Debian 5.10.106-1 (2022-03-17) x86_64 GNU/Linux

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
derklarocommented, May 11, 2022

Setting the system properties you send me (I mentioned them above) increases the likeliness by a lot

0reactions
derklarocommented, May 17, 2022

Should be around 100 - 150. Maybe it is also noteworthy that there are multiple indepent applications running which are all allocating buffers at the same time (the receive the same message around the same time)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build stability fix + .gitignore update by trustin · Pull Request #1
Build stability fix + .gitignore update #1 ... PooledByteBuf.readBytes allocates released buffer instead of a writeable one #12387.
Read more >
Index - Apple
Return an InputStream that wraps the given Buffer . asMultiAddressClientFilter() - Method in interface io.servicetalk.http.api.StreamingHttpClientFilterFactory.
Read more >
Commits · dc6cb7545b1e4651e4928a30267f4f952d47dc94 · 郑建锋 ...
Motivation: ByteBufUtilTest.notEqualsBufferSubsections is testing non-equality but just uses random numbers to assume they will not be equal.
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