netty-http-client icon indicating copy to clipboard operation
netty-http-client copied to clipboard

LEAK: ByteBuf.release() was not called before it's garbage-collected

Open wjzhuf opened this issue 7 years ago • 2 comments
trafficstars

When I use netty-http-client I found this error,when I modified class ResponseHandler ,the error disappears。I do not know if this is a problem, so I will ask。My English is poor。thankx

add :

io.netty.util.ReferenceCountUtil.release(content);

protected void internalReceive(HttpResponseStatus status, HttpHeaders headers, ByteBuf content) { try { if (status.code() > 399) { onErrorResponse(status, headers, content.readCharSequence(content.readableBytes(), CharsetUtil.UTF_8).toString()); return; } MediaType mediaType = null; if (headers.contains(HttpHeaderNames.CONTENT_TYPE)) { try { mediaType = MediaType.parse(headers.get(HttpHeaderNames.CONTENT_TYPE)); } catch (Exception ex) { //do nothing } } T o = mediaType == null ? marshallers.read(type, content) : marshallers.read(type, content, mediaType); _doReceive(status, headers, type.cast(o)); } catch (Exception ex) { content.resetReaderIndex(); try { String s = Streams.readString(new ByteBufInputStream(content), "UTF-8"); onErrorResponse(HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE, headers, s); } catch (IOException ex1) { ex.addSuppressed(ex1); } Exceptions.chuck(ex); } finally { //2018-01-24 修改测试 io.netty.util.ReferenceCountUtil.release(content); latch.countDown(); } }

wjzhuf avatar Jan 24 '18 05:01 wjzhuf

Yes, I understand the problem, and if this fix works for you, great. Thank you for contributing the patch.

However, it is not possible to incorporate it into the code as-is, because it will break many clients which try to use the content after it has been released (that will throw an exception).

With some redesign of the API, it would be possible to change that. For now, the best approach is either to use a non-reference counted ByteBufAllocator, or for the code which receives the ByteBuf to dispose it once it knows it will not use it anymore.

timboudreau avatar Jan 24 '18 10:01 timboudreau

@timboudreau
This increase is currently suitable for my application scenario, 1200 threads running for half an hour without leak problems。thank you for your reply。

wjzhuf avatar Jan 24 '18 23:01 wjzhuf