Here is the current implementation of NettyDataBuffer.retainedSlice: https://github.com/spring-projects/spring-framework/blob/a41f97bc2bd547dbb635f032413fe586968912f8/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java#L265-L269

The above version of NettyDataBuffer.retainedSlice will fail to release the slice returned by this.byteBuf.retainedSlice if this.byteBuf.retainedSlice succeeds but allocation of the new NettyDataBuffer fails.

Here is how NettyDataBuffer.retainedSlice should be fixed:

    @Override
    public NettyDataBuffer retainedSlice(int index, int length) {
        ByteBuf slice = this.byteBuf.retainedSlice(index, length);
        try {
            return new NettyDataBuffer(slice, this.dataBufferFactory);
        } catch(final Throwable ex) {
            // Make sure that the slice is released if the allocation of NettyDataBuffer fails
            try {
                slice.release();
            } catch(IllegalReferenceCountException ex2) {
                // Ignore any IllegalReferenceCountException that gets thrown by slice.release()
            }
            throw ex;
        }
    }

The fixed version of NettyDataBuffer.retainedSlice will ensure that the slice is released if this.byteBuf.retainedSlice succeeds but the allocation of the NettyDataBuffer fails.

Comment From: poutsma

I can only see two possibilities for new NettyDataBuffer(slice, this.dataBufferFactory) failing:

  1. either of the two parameters are null, or
  2. the JVM is out of memory

If we assume that ByteBuf::retainedSlice does not return null, we can rule out option 1, which leaves us with the possibility of an OutOfMemoryError. If the JVM has run out of heap memory, then releasing direct memory is not going to help much.

In short: I see your point, but I don't see any way for this issue to occur. But perhaps I am missing something?

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.