Conversation
…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.
…4, and 3.15 in CI and setup
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.
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 usingid(task)to using actualasyncio.Taskobjects 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:
id(task)to use theasyncio.Taskobject itself as the key intask_sessionsandslot_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]call_soon, ensuringcleanup_tasksis populated immediately aftergatherreturns. (fastapi_async_sqlalchemy/middleware.py)__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:
.github/workflows/ci.yml,setup.py) [1] [2]clstoselfin several places for correctness and consistency. (fastapi_async_sqlalchemy/middleware.py) [1] [2]Testing and versioning:
tests/test_multi_session_fixes.py)0.7.2a1to reflect these changes. (fastapi_async_sqlalchemy/__init__.py)