Skip to content

fix(aio): cursor loops return rows that are None instead of ending at them - #1392

Merged
JuanLeee merged 1 commit into
mainfrom
fix/aio-anext-none-row
Nov 26, 2026
Merged

JuanLeee merged 1 commit into
mainfrom
fix/aio-anext-none-row

Conversation

@JuanLeee

Copy link
Copy Markdown
Contributor

Description

async for row in cursor and anext(cursor) on an AsyncAwsWrapperCursor end at the first row that is None, and the rows after it are lost with no error. A psycopg row is None when scalar_row reads a NULL first column or a custom row factory returns None: 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 one FETCH per row. Both have been live since ecc898e8 (feat: asyncio support (async wrapper, plugins, and SQLAlchemy dialects) (#1257), 2026-07-29), so 3.1.0 has them. aiomysql rows are never None, 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.

  1. AsyncAwsWrapperCursor.__anext__ called the wrapper's plugin-routed fetchone and raised StopAsyncIteration when it returned None, which fetchone returns both at the end and for a None row, so the loop ended at the first such row. __anext__ now awaits anext() on the driver's iterator inside the Cursor.fetchone plugin call, so failover and read/write splitting still see every row. That call turns the driver's StopAsyncIteration into None and records whether it read a row, and the loop ends only when it returns None with no row read. The end reaches plugins as None, because the aio failover plugin catches Exception around the next step.
  2. The wrapper keeps the driver's iterator for the current result in the new _target_iterator. It is made at the first loop read and cleared by execute, executemany, callproc and nextset, 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 old fetchone path, where None ends the rows; no supported driver has that shape.
  3. psycopg's AsyncServerCursor.fetchone sends a FETCH for 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 fetches itersize rows (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

  1. Another anext() after the end raises StopAsyncIteration again; before, anext() after an early end went on with the rows after the None row.
  2. On a psycopg server cursor, the loop reads a page ahead, so a fetch call after a break goes on after that page, as psycopg does: with 250 rows and a break after row 1, fetchone returns row 101 where it returned row 2.
  3. On a psycopg server cursor, errors now come with the page, as psycopg does: a query that fails on its third row raises before the loop returns the first two, and after the backend is terminated mid-loop the loop first returns the rest of its fetched page. The exception class doesn't change.
  4. A plugin that calls the next step, gets a row and then returns None now gives the loop a None row; before, the loop ended there. A plugin that returns None without calling the next step still ends it.

Deliberate divergences from sync

  1. __aiter__ still returns the wrapper, because the aio failover and read/write splitting plugins need every loop row as Cursor.fetchone. The sync __iter__ on main returns the driver's iterator, so sync loop rows skip the plugins.
  2. The sync __next__ that feat: plugin call context for cursor execute arguments #1330 added on dev routes rows as Cursor.__next__, which no aio plugin lists, so the async loop stays on Cursor.fetchone. When the driver's iterator is a separate object, that __next__ falls back to the cursor's fetchone, where None ends the rows; the async loop reads through that iterator.

Not in scope

  1. SQLAlchemy's async stream() on postgresql+aws_wrapper_psycopg returns 0 rows with no error, with and without this change, where postgresql+psycopg returns 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 None rows, 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 without None and 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 the fetchone fallback (1). With this change's two test files on main, 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 -Werror passes 2837 tests on Python 3.12 and 3.10 (2654 on main), with flake8, isort and mypy clean. The change applies to dev at aaed53d0 without 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 on main, all match now, and none that matched changed. 45 of 79 more live cases fail on main, 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.

@JuanLeee
JuanLeee merged commit 511573c into main Nov 26, 2026
9 checks passed
@JuanLeee
JuanLeee deleted the fix/aio-anext-none-row branch November 26, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants