Repository navigation
fix(aio): cursor loops return rows that are None instead of ending at them - #1392
Merged
Merged
Conversation
karenc-bq
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
async for row in cursorandanext(cursor)on anAsyncAwsWrapperCursorend at the first row that isNone, and the rows after it are lost with no error. A psycopg row isNonewhenscalar_rowreads a NULL first column or a custom row factory returnsNone: over the values 1, NULL, 3 the wrapper returns[1]where psycopg returns[1, None, 3], on client and server cursors. The same per-row read makes a loop over a psycopg server cursor send oneFETCHper row. Both have been live sinceecc898e8(feat: asyncio support (async wrapper, plugins, and SQLAlchemy dialects) (#1257), 2026-07-29), so 3.1.0 has them. aiomysql rows are neverNone, and the sync cursor isn't affected.This PR makes the async loop read rows from the driver cursor's own async iterator, as the synchronous cursor already does, and still passes every row through the plugins as
Cursor.fetchone.AsyncAwsWrapperCursor.__anext__called the wrapper's plugin-routedfetchoneand raisedStopAsyncIterationwhen it returnedNone, whichfetchonereturns both at the end and for aNonerow, so the loop ended at the first such row.__anext__now awaitsanext()on the driver's iterator inside theCursor.fetchoneplugin call, so failover and read/write splitting still see every row. That call turns the driver'sStopAsyncIterationintoNoneand records whether it read a row, and the loop ends only when it returnsNonewith no row read. The end reaches plugins asNone, because the aio failover plugin catchesExceptionaround the next step._target_iterator. It is made at the first loop read and cleared byexecute,executemany,callprocandnextset, so a later loop on the same result goes on with the rows it hasn't returned, as on the raw driver. psycopg 3.3 and later and aiomysql cursors are their own iterators. A separate driver iterator is also kept for the whole result, because a new one per row would drop the rows it had read ahead. A driver cursor without__aiter__keeps the oldfetchonepath, whereNoneends the rows; no supported driver has that shape.AsyncServerCursor.fetchonesends aFETCHfor one row, so a loop over a server cursor made one round trip per row. The loop now runs through the server cursor's own__anext__, which fetchesitersizerows (default 100) at a time. For 250 rows a loop now makes 3 round trips, as psycopg does, where it made 251. Client-cursor and aiomysql loops send nothing either way.Behaviour changes
anext()after the end raisesStopAsyncIterationagain; before,anext()after an early end went on with the rows after theNonerow.breakgoes on after that page, as psycopg does: with 250 rows and abreakafter row 1,fetchonereturns row 101 where it returned row 2.Nonenow gives the loop aNonerow; before, the loop ended there. A plugin that returnsNonewithout calling the next step still ends it.Deliberate divergences from sync
__aiter__still returns the wrapper, because the aio failover and read/write splitting plugins need every loop row asCursor.fetchone. The sync__iter__onmainreturns the driver's iterator, so sync loop rows skip the plugins.__next__that feat: plugin call context for cursor execute arguments #1330 added ondevroutes rows asCursor.__next__, which no aio plugin lists, so the async loop stays onCursor.fetchone. When the driver's iterator is a separate object, that__next__falls back to the cursor'sfetchone, whereNoneends the rows; the async loop reads through that iterator.Not in scope
stream()onpostgresql+aws_wrapper_psycopgreturns 0 rows with no error, with and without this change, wherepostgresql+psycopgreturns 5. Core and ORM never reach__anext__, so it is a separate defect.Tests
Of the 183 new unit test cases, 168 are in a new file, written from the expected behaviour before the fix was seen, and run on fakes modelled on psycopg and aiomysql source, plus a separate-iterator cursor: 52 for
Nonerows, 35 for mixed loop and fetch sequences in the raw driver's order, 6 for the separate iterator, 27 for plugins, 15 for driver errors, 8 for the end of the rows, 3 for round trips and 22 for rows withoutNoneand the fetch methods, which don't change. The other 15 cover the wrapper's own iterator, including separate driver iterators (5), the reset on a new result (4) and thefetchonefallback (1). With this change's two test files onmain, 104 of their 241 cases fail (one, a changed test, only on its new setup) and the rest pass. No unit test pins the 3 round trips; the live run measured them.pytest tests/unit -Werrorpasses 2837 tests on Python 3.12 and 3.10 (2654 onmain), with flake8, isort and mypy clean. The change applies todevataaed53d0without a conflict, and its test files pass there.Live checks outside this PR ran against PostgreSQL 18 and MySQL 9.7 with psycopg 3.3.9 and aiomysql 0.3.2. A differential run compared the wrapper with the raw driver on 608 cases under each of three plugin chains (none, a recorder, the defaults), plus the repro and 8 runs of two plugins that stop the loop or return
None(checked against expected results): 1282 of the 1834 results matched onmain, all match now, and none that matched changed. 45 of 79 more live cases fail onmain, and all pass now. psycopg 3.1 and 3.2 weren't tested, and the integration test suite wasn't run. No CHANGELOG entry, message key or docs change.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.