Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
Release resource when exception occur

Release resource for http1
  • Loading branch information
finefuture committed Apr 24, 2024
1 parent 7cf5fe4 commit abad283
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ protected final HttpOutputMessage buildMessage(Object data) throws Throwable {
data = ((HttpResult<?>) data).getBody();
}
HttpOutputMessage outputMessage = encodeHttpOutputMessage(data);
preOutputMessage(outputMessage);
responseEncoder.encode(outputMessage.getBody(), data);
try {
preOutputMessage(outputMessage);
responseEncoder.encode(outputMessage.getBody(), data);
} catch (Throwable t) {
outputMessage.close();
throw t;
}
return outputMessage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.InputStream;

public interface HttpInputMessage {
public interface HttpInputMessage extends AutoCloseable {

InputStream getBody();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

public interface HttpOutputMessage {
public interface HttpOutputMessage extends AutoCloseable {

HttpOutputMessage EMPTY_MESSAGE = new HttpOutputMessage() {

Expand All @@ -29,6 +29,11 @@ public interface HttpOutputMessage {
public OutputStream getBody() {
return INPUT_STREAM;
}

@Override
public void close() throws Exception {
INPUT_STREAM.close();
}
};

OutputStream getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public String method() {
public String path() {
return httpMetadata.path();
}

@Override
public void close() throws Exception {
httpInputMessage.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public InputStream getBody() {
public HttpHeaders headers() {
return httpMetadata.headers();
}

@Override
public void close() throws Exception {
httpInputMessage.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public Http1InputMessage(InputStream body) {
public InputStream getBody() {
return body;
}

@Override
public void close() throws Exception {
body.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.OutputStream;

import io.netty.buffer.ByteBufOutputStream;

public class Http1OutputMessage implements HttpOutputMessage {

private final OutputStream outputStream;
Expand All @@ -32,4 +34,12 @@ public Http1OutputMessage(OutputStream outputStream) {
public OutputStream getBody() {
return outputStream;
}

@Override
public void close() throws Exception {
if (outputStream instanceof ByteBufOutputStream) {
((ByteBufOutputStream) outputStream).buffer().release();
}
outputStream.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public int id() {
public boolean isEndStream() {
return endStream;
}

@Override
public void close() throws Exception {
body.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.OutputStream;

import io.netty.buffer.ByteBufOutputStream;

public class Http2OutputMessageFrame implements Http2OutputMessage {

private final OutputStream body;
Expand All @@ -42,6 +44,14 @@ public OutputStream getBody() {
return body;
}

@Override
public void close() throws Exception {
if (body instanceof ByteBufOutputStream) {
((ByteBufOutputStream) body).buffer().release();
}
body.close();
}

@Override
public boolean isEndStream() {
return endStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.dubbo.remoting.http12.HttpHeaderNames;
import org.apache.dubbo.remoting.http12.HttpInputMessage;
import org.apache.dubbo.remoting.http12.RequestMetadata;
import org.apache.dubbo.remoting.http12.exception.DecodeException;
import org.apache.dubbo.remoting.http12.h1.Http1ServerChannelObserver;
import org.apache.dubbo.remoting.http12.h1.Http1ServerStreamChannelObserver;
import org.apache.dubbo.remoting.http12.h1.Http1ServerTransportListener;
Expand Down Expand Up @@ -82,6 +83,15 @@ protected HttpMessageListener buildHttpMessageListener() {
return new DefaultHttpMessageListener(listeningDecoder);
}

@Override
protected void doOnData(HttpInputMessage message) {
try (HttpInputMessage msg = message) {
super.doOnData(msg);
} catch (Exception e) {
throw new DecodeException(e);
}
}

private ServerCallListener startListener(
RpcInvocation invocation, MethodDescriptor methodDescriptor, Invoker<?> invoker) {
switch (methodDescriptor.getRpcType()) {
Expand Down

0 comments on commit abad283

Please sign in to comment.