title: Debugging IllegalReferenceCountException: refCnt: 0, decrement: 1
body: <p>I have a question to ask.  Is there a good tips/simple way to debug exceptions with <code>IllegalReferenceCountException: refCnt: 0, decrement: 1</code>. I tried many ways to but fails to find a good solution on debugging this. I would like to know whether there's some configuration changes (other than enabling <code>DEBUG</code> on logging, which doesn't have much helpful info) or some code changes I can update to enable more debugging information for such Netty exception.</p>
  <p>Java code</p>
  <pre class="lang-java prettyprint-override"><code>    WebClient.Builder webClientBuilder = WebClient.builder();
  
      if (ENDPOINTS_REQUIRE_PROXY.contains(endpoint)) {
        ReactorClientHttpConnector proxyConnector =
            new ReactorClientHttpConnector(proxyHTTPClient);
        webClientBuilder.clientConnector(proxyConnector);
      }
  
      return requestBodySpec
            .header(&quot;Content-Type&quot;, HEADER_VALUE_CONTENT_TYPE)
            .body(Mono.just(requestBody), requestBodyClass)
            .retrieve()
            .bodyToMono(String.class)
            .timeout(Duration.ofMillis(alloyConfig.getTimeoutInMilliseconds()))
            .block();
  </code></pre>
  <h2>Environments</h2>
  <ul>
  <li>Reactor version(s) used:
  <ul>
  <li><code>org.projectreactor:reactor-spring:1.0.1.RELEASE</code></li>
  <li><code>org.springframework.boot' version '2.3.4.RELEASE</code></li>
  <li><code>io.spring.dependency-management' version '1.0.9.RELEASE</code></li>
  </ul>
  </li>
  <li>JVM version (<code>java -version</code>):</li>
  </ul>
  <pre><code>openjdk version &quot;11.0.2&quot; 2019-01-15
  OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
  OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
  </code></pre>
  <ul>
  <li>OS and version (eg. <code>uname -a</code>):
  <code>Darwin 19.6.0 Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64 x86_64</code></li>
  </ul>
  <pre><code>
  2020-12-11 00:29:12.498 ERROR 11918 --- [ctor-http-nio-2] r.n.channel.ChannelOperationsHandler     : [id: 0xebbe5eff, L:0.0.0.0/0.0.0.0:55189] Error was received while reading the incoming data. The connection will be closed.
  
  io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
          at io.netty.util.internal.ReferenceCountUpdater.toLiveRealRefCnt(ReferenceCountUpdater.java:74)
          at io.netty.util.internal.ReferenceCountUpdater.release(ReferenceCountUpdater.java:138)
          at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:100)
          at io.netty.handler.codec.http.DefaultHttpContent.release(DefaultHttpContent.java:92)
          at io.netty.util.ReferenceCountUtil.release(ReferenceCountUtil.java:88)
          at reactor.netty.channel.FluxReceive.onInboundNext(FluxReceive.java:340)
          at reactor.netty.channel.ChannelOperations.onInboundNext(ChannelOperations.java:358)
          at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:647)
          at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:96)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
          at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
          at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
          at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1526)
          at io.netty.handler.ssl.SslHandler.decodeNonJdkCompatible(SslHandler.java:1287)
          at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1324)
          at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:501)
          at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:440)
          at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.handler.proxy.ProxyHandler.channelRead(ProxyHandler.java:253)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
          at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253)
          at io.netty.handler.proxy.HttpProxyHandler$HttpClientCodecWrapper.channelRead(HttpProxyHandler.java:272)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
          at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
          at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
          at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
          at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
          at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
          at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
          at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
          at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
          at java.base/java.lang.Thread.run(Thread.java:834)
  
  2020-12-11 00:29:12.498 [reactor-http-nio-2] ERROR r.n.channel.ChannelOperationsHandler - [id: 0xebbe5eff, L:0.0.0.0/0.0.0.0:55189] Error was received while reading the incoming data. The connection will be closed.
  io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1
          at io.netty.util.internal.ReferenceCountUpdater.toLiveRealRefCnt(ReferenceCountUpdater.java:74)
          at io.netty.util.internal.ReferenceCountUpdater.release(ReferenceCountUpdater.java:138)
          at io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:100)
          at io.netty.handler.codec.http.DefaultHttpContent.release(DefaultHttpContent.java:92)
          at io.netty.util.ReferenceCountUtil.release(ReferenceCountUtil.java:88)
          at reactor.netty.channel.FluxReceive.onInboundNext(FluxReceive.java:340)
          at reactor.netty.channel.ChannelOperations.onInboundNext(ChannelOperations.java:358)
          at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:647)
          at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:96)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
          at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
          at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
          at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1526)
          at io.netty.handler.ssl.SslHandler.decodeNonJdkCompatible(SslHandler.java:1287)
          at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1324)
          at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:501)
          at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:440)
          at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.handler.proxy.ProxyHandler.channelRead(ProxyHandler.java:253)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
          at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253)
          at io.netty.handler.proxy.HttpProxyHandler$HttpClientCodecWrapper.channelRead(HttpProxyHandler.java:272)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
          at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
          at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
          at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
          at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
          at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714)
          at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650)
          at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576)
          at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
          at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
          at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
          at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
          at java.base/java.lang.Thread.run(Thread.java:834)
  
  </code></pre>