-
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Netty - PooledByteBufAllocator and Unpooled.wrappedBuffer method - potential bug? #8103
Comments
@felixunivers I am not sure if I correctly understand the problem but if I understood it correctly you are surprised that calling I can see why this may be confusing and could lead to surprising and hard to debug code. Let me do a pr to change this and discuss with the other developers in this pr. |
@felixunivers so actually after thinking a bit more about it and reading our java docs again I think it works as expected... Changing it would be surprising to people that expect it to work like documented. That said we just deprecated these methods in favour of using WDYT? |
Norman's explanation and my further experience with Netty's buffers shows that releasing empty buffers appears as a logical approach for easier/automatic handling of memory for ByteBuf-s. My goal here was to create one dynamic wrapped buffer based on 2 or more underlying/member buffers. For example buf0 is the wrapped/container buffer and buf1, buf2, ... are the member buffers. When any of the member buffers get updated we see the combined result in buf0.
From my testing I am seeing that wrappedBuffer can effectively be used only for the bytes that have been written to the member buffers before the wrappedBuffer - buf0 was created. The writerIndex of each member buffer determines the resulting stream of bytes in wrappedBuffer - buf0 at the time of its creation. If bytes in the member buffers change, these changes are reflected in the wrappedBuffer - buf0, BUT only up to the writerIndex of each member buffer at the time of wrappedBuffer - buf0 creation. This surely works as designed. Perhaps the documentation could be updated to make this point clear to users. (For the authors - Feel free to use my description and modify as you see fit for the documentation). Perhaps, when you Netty gentlemen get bored - a "DynamicWrappedBuffer" that reflects all changes in the member buffers dynamically / instantly could be a cool buffer to add. (Likely a bit PITA to design though). |
They take contributions FYI. They probably hardly get bored. 😄 |
Closing as it seems like all is working as designed |
After allocating some direct buffers and trying them to be combined into a wrapped buffer,
the referenced initial buffers become unusable / freed. Example:
If we now do:
bb0 is shown by debugger as 'freed', .refCnt() returns 0, and the next line - bb0.writeBytes(x) throws exception:
io.netty.util.IllegalReferenceCountException: refCnt: 0
BUT, if we write some data into the buffer before we call the wrappedBuffer(...) method,
all appears to be working fine:
BUT, since we did not write any data to the 2nd buffer, the problem remains there.
ByteBuf bb2 = BUFFERS.get(2);
System.out.println(bb2.refCnt()); // returns 0 !!!
Perhaps this is a feature to auto-release unused memory, but in this case it presents itself rather as a bug. Hopefully it is something Netty designers/insiders could clear up for us.
The text was updated successfully, but these errors were encountered: