[JAVA-2379] If closing a cursor throws, subsequent calls will throw java.lang.IllegalStateException Created: 09/Nov/16  Updated: 21/Nov/16  Resolved: 21/Nov/16

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: 3.3.0
Fix Version/s: 3.4.0

Type: Bug Priority: Major - P3
Reporter: Jackson Davis Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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



 Comments   
Comment by Ross Lawley [ 21/Nov/16 ]

Thanks jcdavis for pointing this out, it is fixed in master and will be released as part of 3.4.0.

Comment by Githook User [ 21/Nov/16 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Mark the QueryBatchCursor closed before calling killCursor

This ensures if there is an error killing the cursor subsequent calls to the
cursor will act as expected for a closed cursor.

JAVA-2379
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/f7a27797dfa49e86995f064cb223fca3dff6f0be

Comment by Jackson Davis [ 09/Nov/16 ]

In case its of interest, here is the underlying exception that caused close to fail the first time. Doesn't seem super interesting to me though, just a mongod not being up

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:49) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:384) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:101) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:87) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.connection.DefaultServer.getConnection(DefaultServer.java:86) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:86) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.operation.QueryBatchCursor.killCursor(QueryBatchCursor.java:263) ~[3rdparty.mongodb-0.jar:na]
    at com.mongodb.operation.QueryBatchCursor.close(QueryBatchCursor.java:148) ~[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]

Generated at Thu Feb 08 08:57:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.