Skip to content

Commit

Permalink
Fix memory leak in HttpPostRequestDecoder when body is empty (#14760)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxsean authored Oct 11, 2024
1 parent d5873cb commit 61d7f2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpHeaders;
Expand Down Expand Up @@ -169,7 +168,7 @@ public static HttpPostRequestDecoder createPostRequestDecoder(
inputStream.mark(Integer.MAX_VALUE);
}
if (inputStream.available() == 0) {
data = Unpooled.EMPTY_BUFFER;
return null;
} else {
data = HEAP_ALLOC.buffer();
ByteBufOutputStream os = new ByteBufOutputStream(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import org.apache.dubbo.rpc.protocol.tri.rest.test.BaseServiceTest
import org.apache.dubbo.rpc.protocol.tri.test.TestRequest
import org.apache.dubbo.rpc.protocol.tri.test.TestRunnerBuilder

import io.netty.buffer.AbstractByteBuf
import io.netty.util.ResourceLeakDetector

class RestProtocolTest extends BaseServiceTest {

@Override
Expand Down Expand Up @@ -174,18 +177,28 @@ class RestProtocolTest extends BaseServiceTest {
'/argTest' | 'Sam is 8 years old'
}

@SuppressWarnings('GroovyAccessibility')
def "urlEncodeForm body test"() {
given:
def level = ResourceLeakDetector.level
def leaks = AbstractByteBuf.leakDetector.allLeaks
ResourceLeakDetector.level = ResourceLeakDetector.Level.PARANOID
leaks.clear()
and:
def request = new TestRequest(
path: path,
contentType: MediaType.APPLICATION_FROM_URLENCODED,
body: body
)
expect:
runner.post(request) == output
leaks.empty
cleanup:
ResourceLeakDetector.level = level
where:
path | body | output
'/argTest' | 'name=Sam&age=8' | 'Sam is 8 years old'
'/argTest' | '' | 'null is 0 years old'
}

def "override mapping test"() {
Expand Down

0 comments on commit 61d7f2e

Please sign in to comment.