Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2379

If closing a cursor throws, subsequent calls will throw java.lang.IllegalStateException

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.4.0
    • Affects Version/s: 3.3.0
    • Component/s: Query Operations
    • None

      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:

      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 
      

      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

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jcdavis Jackson Davis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: