GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Handle CancelledError in bind_execute_many by MaciejZet · Pull Request #1375 · MagicStack/asyncpg

Conversation

@MaciejZet

@MaciejZet MaciejZet commented Sep 29, 2026

Copy link
Copy Markdown

Fixes #1265

bind_execute_many catches Exception. CancelledError is a BaseException, so a cancelled executemany skips that handler and reaches return await waiter. The server is still in ClientRead, waiting for the rest of the batch, and that waiter never completes. asyncio.gather then hangs.

The cancel path now stores the CancelledError on the waiter and aborts the protocol. The same abort runs when cancellation arrives while the method is already waiting on the server. The connection closes. A timeout still sends the existing fail message and keeps the connection.

Test plan

  • tests.test_cancellation — 5 tests, including a new executemany case that cancels a batch larger than one 128KB write and expects CancelledError within 5 seconds with the connection closed
  • tests.test_execute.TestExecuteMany — 13 tests, including timeout and client-failure cases

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
MaciejZet added 2 commits October 9, 2026 00:07
@MaciejZet
Handle CancelledError in bind_execute_many
130386d
Fixes MagicStack#1265
@MaciejZet
fix: preserve connection on executemany cancellation
b9f7518
@MaciejZet
MaciejZet force-pushed the fix/executemany-cancelled branch from 8ef7885 to b9f7518 Compare October 8, 2026 22:13
@MaciejZet

MaciejZet commented Oct 14, 2026

Copy link
Copy Markdown
Author

All three local pool regressions pass at b9f7518. With max_size=1, cancellation during the executemany write-loop raises CancelledError, closes the connection, and allows a clean release and subsequent acquire. SELECT 42 succeeds on the replacement connection; the batch rolls back and a previously committed row remains. Upstream 13b0191 leaves the task pending and release raises InterfaceError before the pool recovers.

One description clarification: connection closure applies to write-loop cancellation. Cancellation while waiting on the server, and the write-loop timeout control, preserve a usable connection in these tests. Tested locally with native modules, Python 3.12 and PostgreSQL 17.10; not the full asyncpg suite.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

CancelledError not correctly handled in bind_execute_many() (was: unusual asyncio-related deadlock seen with gather() where one or more tasks are failing)

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@MaciejZet
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.