[JAVA-155] StackOverflowError when fetching a tailable cursor after getting the last document Created: 25/Aug/10  Updated: 29/Oct/10  Resolved: 15/Sep/10

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: 2.1
Fix Version/s: 2.2

Type: Bug Priority: Major - P3
Reporter: Sergey Shinderuk Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Sun Java 1.6_20 (64bit)

db version v1.6.1, pdfile version 4.5
git hash: c5f5f9a4f3b515dfd5272d373093fd4fd58c95d9
sys info: Linux xxxx 2.6.32-24-server #39-Ubuntu SMP Wed Jul 28
06:21:40 UTC 2010 x86_64 BOOST_LIB_VERSION=1_40



 Description   

When iterating through a capped collection via a tailable cursor, I get a StackOverflowError as soon as the cursor reaches the end of the collection.

Error can be reproduced by running the code below:

package test;

import com.mongodb.*;

public class Test {

public static void main(String[] args) throws Exception {

Mongo mongo = new Mongo("localhost");
DB db = mongo.getDB("test");
DBCollection coll = db.getCollection("mycoll");

DBCursor cur = coll.find().sort(new BasicDBObject("$natural", 1))
.addOption(Bytes.QUERYOPTION_TAILABLE);

while (cur.hasNext())

{ System.out.println(cur.next()); }

}
}

After reading the last document, cursor.hasNext() will result in a StackOverflowError. Here is the stack trace:

Exception in thread "main" java.lang.StackOverflowError
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at org.bson.io.Bits.readFully(Bits.java:30)
at com.mongodb.Response.<init>(Response.java:34)
at com.mongodb.DBPort.go(DBPort.java:85)
at com.mongodb.DBPort.call(DBPort.java:56)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:186)
at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:173)
at com.mongodb.DBApiLayer$Result._advance(DBApiLayer.java:328)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:311)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
at com.mongodb.DBApiLayer$Result.hasNext(DBApiLayer.java:312)
<...>

The problem is shadowed if a cursor is created with QUERYOPTION_AWAITDATA option. The stack will nevertheless fill up because of recursion, but much slower.



 Comments   
Comment by auto [ 22/Sep/10 ]

Author:

{'login': 'erh', 'name': 'Eliot Horowitz', 'email': 'eliot@10gen.com'}

Message: switch from recursion to while loop to avoid inf. recursion JAVA-155
http://github.com/mongodb/mongo-java-driver/commit/c3e2a75f02778a40b9869bbb21d9abfeb03bb694

Comment by Scott Hernandez (Inactive) [ 15/Sep/10 ]

http://github.com/scotthernandez/mongo-java-driver/commit/66952e18ea560a4befbd07e0a48c852ec9040084

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