Skip to content

Commit 5f1ee38

Browse files
committed
Call close before raising (Async)StopIteration
1 parent bf4c24d commit 5f1ee38

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

pymongo/asynchronous/cursor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,10 +1263,14 @@ async def next(self) -> _DocumentType:
12631263
self._exhaust_checked = True
12641264
await self._supports_exhaust()
12651265
if self._empty:
1266+
if self._cursor_type == CursorType.NON_TAILABLE:
1267+
await self.close()
12661268
raise StopAsyncIteration
12671269
if len(self._data) or await self._refresh():
12681270
return self._data.popleft()
12691271
else:
1272+
if self._cursor_type == CursorType.NON_TAILABLE:
1273+
await self.close()
12701274
raise StopAsyncIteration
12711275

12721276
async def _next_batch(self, result: list, total: Optional[int] = None) -> bool: # type: ignore[type-arg]

pymongo/synchronous/cursor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,10 +1261,14 @@ def next(self) -> _DocumentType:
12611261
self._exhaust_checked = True
12621262
self._supports_exhaust()
12631263
if self._empty:
1264+
if self._cursor_type == CursorType.NON_TAILABLE:
1265+
self.close()
12641266
raise StopIteration
12651267
if len(self._data) or self._refresh():
12661268
return self._data.popleft()
12671269
else:
1270+
if self._cursor_type == CursorType.NON_TAILABLE:
1271+
self.close()
12681272
raise StopIteration
12691273

12701274
def _next_batch(self, result: list, total: Optional[int] = None) -> bool: # type: ignore[type-arg]

0 commit comments

Comments
 (0)