-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: None
When a cursor returns a single document in the firstBatch and the cursor is exhausted, we do not clean the cursor up. The following test demonstrates this behavior:
it.only('should close the cursor when there are no documents remaining', async function () { await client.connect(); await client.db('foo') .collection('bar') .insertOne({ name: 'bailey' }); const cursor = client.db('foo') .collection('bar') .find({}); cursor.limit(1); cursor.batchSize(-1); const doc = await cursor.next(); expect(doc, 'expected a result but none was received').to.exist; expect( cursor.closed, 'expected cursor to be closed but it was not' ).to.be.true; });
Root Cause
In abstract_cursor.ts' next method, if the cursor is not initialized, we:
- initialize the cursor
- return the first document
After returning the first document, we don't check if the cursor is exhausted and close it if it is.
Unknowns
Is this a bug or works as designed? We tell users they must always close their cursors, so we could demand that users iterate their cursors until they're complete (another call to `next` will close the cursor in the above test). However, in `findOne` we construct a cursor and immediately return the result of a single document cursor without closing it. So we either need to fix the bug outlined in this ticket, or ensure we close the cursor we're creating in `findOne`.
AC
Implementation
- Add handling for cursor exhaustion upon firstBatch, if id == 0, cleanup.
Testing
- closes the cursor if it is exhausted on the firstBatch