Skip to content

Fix/pagination infinite loop - #2682

Open
AzmainMahatab wants to merge 2 commits into
cloudflare:nextfrom
AzmainMahatab:fix/pagination-infinite-loop
Open

Fix/pagination infinite loop#2682
AzmainMahatab wants to merge 2 commits into
cloudflare:nextfrom
AzmainMahatab:fix/pagination-infinite-loop

Conversation

@AzmainMahatab

@AzmainMahatab AzmainMahatab commented Jan 9, 2026

Copy link
Copy Markdown
  • I understand that this repository is auto-generated, and my pull request may not be merged

Changes being requested

This PR fixes a critical logic bug in pagination.py that causes infinite loops when iterating over results.

The Bug:
The next_page_info iterator blindly increments the page number even when the API returns the last page's data repeatedly for out-of-bounds requests (instead of 404s).

The Fix:

  1. Updated Sync and Async paginators to strictly stop iteration when current_page >= total_pages.
  2. Updated V4PagePaginationArrayResultInfo model to include the missing total_pages, total_count, and count fields so the SDK can correctly read the metadata.

Verification:

  • Verified locally with a reproduction script against the live client.accounts.list() endpoint.
  • Added unit tests in tests/test_pagination_fix.py to verify the logic in isolation.

Additional context & links

Example codes to test:

from cloudflare import Cloudflare, AsyncCloudflare

api_token = "redacted"
cloudflare = Cloudflare(api_token=api_token)

for account in cloudflare.accounts.list():
    print(account.name)

async_cloudflare = AsyncCloudflare(api_token=api_token)

async for account in async_cloudflare.accounts.list():
    print(account .name)

It was looping indefinitely by printing the same account, but with my fix, the loop properly ends.

Note on CI:
I noticed unrelated CI failures:

  • ImportError in radar tests (circular imports upstream).
  • AuthenticationError in cloudforce_one tests (upstream mock server auth).

My specific unit tests for pagination pass, and the formatting checks are green.

@AzmainMahatab
AzmainMahatab force-pushed the fix/pagination-infinite-loop branch from bc65071 to ad9a22a Compare January 9, 2026 08:40
@AzmainMahatab
AzmainMahatab changed the base branch from main to next January 9, 2026 08:44
@stainless-app
stainless-app Bot force-pushed the next branch 7 times, most recently from a1e1bee to 87ec5d0 Compare July 13, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant