Skip to content

0.7.2a1#41

Open
h0rn3t wants to merge 9 commits intomainfrom
0.7.2a1
Open

0.7.2a1#41
h0rn3t wants to merge 9 commits intomainfrom
0.7.2a1

Conversation

@h0rn3t
Copy link
Copy Markdown
Owner

@h0rn3t h0rn3t commented Mar 24, 2026

This pull request introduces several significant improvements and bug fixes to the multi-session handling logic in fastapi_async_sqlalchemy, as well as updates Python version support and testing. The most important changes include switching from using id(task) to using actual asyncio.Task objects as keys in session tracking, correcting cleanup task scheduling to avoid race conditions, and ensuring robust error aggregation during session cleanup. Additionally, support for Python 3.13, 3.14, and 3.15 is added, and comprehensive regression tests are introduced.

Multi-session handling and bug fixes:

  • Changed all uses of id(task) to use the asyncio.Task object itself as the key in task_sessions and slot_holders, preventing subtle bugs due to memory address reuse when tasks are garbage collected. (fastapi_async_sqlalchemy/middleware.py) [1] [2] [3] [4] [5] [6] [7] [8] [9]
  • Fixed a race condition in cleanup task scheduling by directly creating cleanup tasks in the done callback, rather than deferring with call_soon, ensuring cleanup_tasks is populated immediately after gather returns. (fastapi_async_sqlalchemy/middleware.py)
  • Updated __aexit__ for single-session mode to use _finalize_session, aggregating errors from both rollback and close operations for more robust error reporting. (fastapi_async_sqlalchemy/middleware.py)

API and compatibility improvements:

  • Added support for Python 3.13, 3.14, and 3.15 in both the CI workflow and package metadata. (.github/workflows/ci.yml, setup.py) [1] [2]
  • Changed method signatures from cls to self in several places for correctness and consistency. (fastapi_async_sqlalchemy/middleware.py) [1] [2]

Testing and versioning:

  • Added a new regression test suite covering the above multi-session bug fixes and error aggregation logic. (tests/test_multi_session_fixes.py)
  • Updated the package version to 0.7.2a1 to reflect these changes. (fastapi_async_sqlalchemy/__init__.py)

Eugene Shershen and others added 9 commits March 25, 2026 00:25
…anup_callback

call_soon deferred cleanup task creation to the next event loop iteration,
causing cleanup_tasks to be empty when __aexit__ gathered them immediately
after child tasks completed. Using create_task() directly ensures cleanup_tasks
is populated synchronously in the done_callback.
id(task) returns the CPython memory address which can be reused after a task
is garbage collected. A new task at the same address would have its session
erroneously removed by an old cleanup callback. Using Task objects directly
as dict/set keys avoids this entirely — two distinct Task objects are never
identical even if they share a memory address at different points in time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The non-multi sessions cleanup path in __aexit__ duplicated _finalize_session
logic without the is_active check or error aggregation. Now both paths use
the same _finalize_session function, ensuring consistent behavior and a single
place to fix future session-cleanup bugs.
…ther

These are regular metaclass instance methods (not @classmethod), so the
conventional name for the first parameter is self, not cls.
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.

1 participant