java.lang.IllegalStateException: Attempted to decrement the reference count below 0

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • We are using the mongoDB async java driver version 3.0.1. When doing performance tests (only reads) with a lot of concurrent requests to the driver, we sometimes encounter the following exception: {code} java.lang.IllegalStateException: Attempted to decrement the reference count below 0 at com.mongodb.binding.AbstractReferenceCounted.release(AbstractReferenceCounted.java:39) at com.mongodb.binding.AsyncClusterBinding$AsyncClusterBindingConnectionSource.release(AsyncClusterBinding.java:113) at com.mongodb.operation.AsyncQueryBatchCursor.killCursor(AsyncQueryBatchCursor.java:154) at com.mongodb.operation.AsyncQueryBatchCursor.close(AsyncQueryBatchCursor.java:79) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:93) at com.mongodb.async.client.OperationIterable.loopCursor(OperationIterable.java:130) at com.mongodb.async.client.OperationIterable.access$000(OperationIterable.java:30) at com.mongodb.async.client.OperationIterable$4.onResult(OperationIterable.java:141) at com.mongodb.async.client.OperationIterable$4.onResult(OperationIterable.java:130) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:90) at com.mongodb.async.client.OperationIterable.loopCursor(OperationIterable.java:130) at com.mongodb.async.client.OperationIterable.access$000(OperationIterable.java:30) at com.mongodb.async.client.OperationIterable$1.onResult(OperationIterable.java:51) at com.mongodb.async.client.OperationIterable$1.onResult(OperationIterable.java:45) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.async.client.MongoClientImpl$2$1.onResult(MongoClientImpl.java:129) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.operation.OperationHelper$ConnectionReleasingWrappedCallback.onResult(OperationHelper.java:140) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:433) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:427) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor$1.onResult(DefaultServer.java:171) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.QueryResultCallback.callCallback(QueryResultCallback.java:64) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:48) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:23) at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:446) at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:440) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:127) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:123) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.InternalStreamConnection.executeCallbackAndReceiveResponse(InternalStreamConnection.java:375) at com.mongodb.connection.InternalStreamConnection.access$1700(InternalStreamConnection.java:65) at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:417) at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:386) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onSuccess(InternalStreamConnection.java:555) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.access$2200(InternalStreamConnection.java:514) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:577) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:561) at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444) at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:441) at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:217) at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:204) at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126) at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:281) at sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ReadTask.completed(WindowsAsynchronousSocketChannelImpl.java:579) at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:397) at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) {code} The IllegalStateException is passed into the callback when executing the following call chain: {code} searchCollection.find() .limit(10). .into(resultList, callback); {code}
    via by Jeff Yemin,
  • We are using the mongoDB async java driver version 3.0.1. When doing performance tests (only reads) with a lot of concurrent requests to the driver, we sometimes encounter the following exception: {code} java.lang.IllegalStateException: Attempted to decrement the reference count below 0 at com.mongodb.binding.AbstractReferenceCounted.release(AbstractReferenceCounted.java:39) at com.mongodb.binding.AsyncClusterBinding$AsyncClusterBindingConnectionSource.release(AsyncClusterBinding.java:113) at com.mongodb.operation.AsyncQueryBatchCursor.killCursor(AsyncQueryBatchCursor.java:154) at com.mongodb.operation.AsyncQueryBatchCursor.close(AsyncQueryBatchCursor.java:79) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:93) at com.mongodb.async.client.OperationIterable.loopCursor(OperationIterable.java:130) at com.mongodb.async.client.OperationIterable.access$000(OperationIterable.java:30) at com.mongodb.async.client.OperationIterable$4.onResult(OperationIterable.java:141) at com.mongodb.async.client.OperationIterable$4.onResult(OperationIterable.java:130) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:90) at com.mongodb.async.client.OperationIterable.loopCursor(OperationIterable.java:130) at com.mongodb.async.client.OperationIterable.access$000(OperationIterable.java:30) at com.mongodb.async.client.OperationIterable$1.onResult(OperationIterable.java:51) at com.mongodb.async.client.OperationIterable$1.onResult(OperationIterable.java:45) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.async.client.MongoClientImpl$2$1.onResult(MongoClientImpl.java:129) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.operation.OperationHelper$ConnectionReleasingWrappedCallback.onResult(OperationHelper.java:140) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:433) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:427) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor$1.onResult(DefaultServer.java:171) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.QueryResultCallback.callCallback(QueryResultCallback.java:64) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:48) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:23) at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:446) at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:440) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:127) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:123) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.InternalStreamConnection.executeCallbackAndReceiveResponse(InternalStreamConnection.java:375) at com.mongodb.connection.InternalStreamConnection.access$1700(InternalStreamConnection.java:65) at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:417) at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:386) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onSuccess(InternalStreamConnection.java:555) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.access$2200(InternalStreamConnection.java:514) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:577) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:561) at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444) at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:441) at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:217) at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:204) at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126) at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:281) at sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ReadTask.completed(WindowsAsynchronousSocketChannelImpl.java:579) at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:397) at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) {code} The IllegalStateException is passed into the callback when executing the following call chain: {code} searchCollection.find() .limit(10). .into(resultList, callback); {code}
    via by Jeff Yemin,
  • Getting the following stacktrace (operation still succeeds) when calling FindFluent#first {noformat} WARNING: Exception calling callback java.lang.IllegalStateException: Attempted to decrement the reference count below 0 at com.mongodb.binding.AbstractReferenceCounted.release(AbstractReferenceCounted.java:23) at com.mongodb.binding.AsyncClusterBinding$AsyncClusterBindingConnectionSource.release(AsyncClusterBinding.java:111) at com.mongodb.operation.OperationHelper$3$1.onResult(OperationHelper.java:219) at com.mongodb.async.SingleResultFuture.init(SingleResultFuture.java:102) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:474) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:468) at com.mongodb.async.SingleResultFuture.init(SingleResultFuture.java:102) at com.mongodb.connection.SingleResultFutureCallback.onResult(SingleResultFutureCallback.java:32) at com.mongodb.connection.QueryResultCallback.callCallback(QueryResultCallback.java:73) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:49) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:24) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:131) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:127) at com.mongodb.connection.InternalStreamConnection.processPendingResults(InternalStreamConnection.java:525) at com.mongodb.connection.InternalStreamConnection.processPendingReads(InternalStreamConnection.java:410) at com.mongodb.connection.InternalStreamConnection.access$1200(InternalStreamConnection.java:56) at com.mongodb.connection.InternalStreamConnection$3.onResult(InternalStreamConnection.java:445) at com.mongodb.connection.InternalStreamConnection$3.onResult(InternalStreamConnection.java:435) {noformat}
    via by Nick Scavelli,
  • Getting the following stacktrace (operation still succeeds) when calling FindFluent#first {noformat} WARNING: Exception calling callback java.lang.IllegalStateException: Attempted to decrement the reference count below 0 at com.mongodb.binding.AbstractReferenceCounted.release(AbstractReferenceCounted.java:23) at com.mongodb.binding.AsyncClusterBinding$AsyncClusterBindingConnectionSource.release(AsyncClusterBinding.java:111) at com.mongodb.operation.OperationHelper$3$1.onResult(OperationHelper.java:219) at com.mongodb.async.SingleResultFuture.init(SingleResultFuture.java:102) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:474) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:468) at com.mongodb.async.SingleResultFuture.init(SingleResultFuture.java:102) at com.mongodb.connection.SingleResultFutureCallback.onResult(SingleResultFutureCallback.java:32) at com.mongodb.connection.QueryResultCallback.callCallback(QueryResultCallback.java:73) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:49) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:24) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:131) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:127) at com.mongodb.connection.InternalStreamConnection.processPendingResults(InternalStreamConnection.java:525) at com.mongodb.connection.InternalStreamConnection.processPendingReads(InternalStreamConnection.java:410) at com.mongodb.connection.InternalStreamConnection.access$1200(InternalStreamConnection.java:56) at com.mongodb.connection.InternalStreamConnection$3.onResult(InternalStreamConnection.java:445) at com.mongodb.connection.InternalStreamConnection$3.onResult(InternalStreamConnection.java:435) {noformat}
    via by Nick Scavelli,
  • If closing a QueryBatchCursor, if the first call throws it counts down a latch but does not set closed to true, meaning all subsequent calls throw an IllegalStateException. Relevant parts of the stack trace, which occurred after the connected mongod was rolled: {code} java.lang.IllegalStateException: Attempted to decrement the reference count below 0 at com.mongodb.binding.AbstractReferenceCounted.release(AbstractReferenceCounted.java:39) ~[3rdparty.mongodb-0.jar:na] at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.release(ClusterBinding.java:97) ~[3rdparty.mongodb-0.jar:na] at com.mongodb.operation.QueryBatchCursor.close(QueryBatchCursor.java:151) ~[3rdparty.mongodb-0.jar:na] at com.mongodb.MongoBatchCursorAdapter.close(MongoBatchCursorAdapter.java:41) ~[3rdparty.mongodb-0.jar:na] at com.mongodb.DBCursor.close(DBCursor.java:603) ~[3rdparty.mongodb-0.jar:na] at {code} Relevant code: https://github.com/mongodb/mongo-java-driver/blob/12895970ae008b4d83378f79dc8fdd54208084c5/driver-core/src/main/com/mongodb/operation/QueryBatchCursor.java#L143 I'm not sure whether or not this is expected behavior, but this is a regression from 2.13, where QueryResultIterator set closed=true before calling killCursor: https://github.com/mongodb/mongo-java-driver/blob/2.13.x/src/main/com/mongodb/QueryResultIterator.java#L176 (and we never observed this issue on 2.x) The obvious fix seems to be moving the closed=true into the finally block or before the try (as per 2.x), but I'm not sure what exactly the expected behavior is when calling close on the same cursor multiple times
    via by Jackson Davis,
    • java.lang.IllegalStateException: Attempted to decrement the reference count below 0 at com.mongodb.binding.AbstractReferenceCounted.release(AbstractReferenceCounted.java:39) at com.mongodb.binding.AsyncClusterBinding$AsyncClusterBindingConnectionSource.release(AsyncClusterBinding.java:113) at com.mongodb.operation.AsyncQueryBatchCursor.killCursor(AsyncQueryBatchCursor.java:154) at com.mongodb.operation.AsyncQueryBatchCursor.close(AsyncQueryBatchCursor.java:79) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:93) at com.mongodb.async.client.OperationIterable.loopCursor(OperationIterable.java:130) at com.mongodb.async.client.OperationIterable.access$000(OperationIterable.java:30) at com.mongodb.async.client.OperationIterable$4.onResult(OperationIterable.java:141) at com.mongodb.async.client.OperationIterable$4.onResult(OperationIterable.java:130) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:90) at com.mongodb.async.client.OperationIterable.loopCursor(OperationIterable.java:130) at com.mongodb.async.client.OperationIterable.access$000(OperationIterable.java:30) at com.mongodb.async.client.OperationIterable$1.onResult(OperationIterable.java:51) at com.mongodb.async.client.OperationIterable$1.onResult(OperationIterable.java:45) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.async.client.MongoClientImpl$2$1.onResult(MongoClientImpl.java:129) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.operation.OperationHelper$ConnectionReleasingWrappedCallback.onResult(OperationHelper.java:140) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:433) at com.mongodb.operation.FindOperation$2$1.onResult(FindOperation.java:427) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor$1.onResult(DefaultServer.java:171) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.QueryResultCallback.callCallback(QueryResultCallback.java:64) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:48) at com.mongodb.connection.ResponseCallback.onResult(ResponseCallback.java:23) at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:446) at com.mongodb.connection.DefaultConnectionPool$PooledConnection$2.onResult(DefaultConnectionPool.java:440) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:127) at com.mongodb.connection.UsageTrackingInternalConnection$3.onResult(UsageTrackingInternalConnection.java:123) at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:53) at com.mongodb.connection.InternalStreamConnection.executeCallbackAndReceiveResponse(InternalStreamConnection.java:375) at com.mongodb.connection.InternalStreamConnection.access$1700(InternalStreamConnection.java:65) at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:417) at com.mongodb.connection.InternalStreamConnection$ResponseBuffersCallback.onResult(InternalStreamConnection.java:386) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.onSuccess(InternalStreamConnection.java:555) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback.access$2200(InternalStreamConnection.java:514) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:577) at com.mongodb.connection.InternalStreamConnection$ResponseHeaderCallback$ResponseBodyCallback.onResult(InternalStreamConnection.java:561) at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:444) at com.mongodb.connection.InternalStreamConnection$3.completed(InternalStreamConnection.java:441) at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:217) at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:204) at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126) at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:281) at sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ReadTask.completed(WindowsAsynchronousSocketChannelImpl.java:579) at sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:397) at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

    Users with the same issue

    Alireza Mohamadi
    Alireza Mohamadi1 times, last one,