File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,10 @@ def commit(self, session: Session) -> None:
7373 raise
7474
7575
76+ # Reference: https://docs.astral.sh/ruff/rules/asyncio-dangling-task/
77+ _background_asyncio_tasks = set ()
78+
79+
7680class AsyncSessionHandler :
7781 scoped_session : async_scoped_session
7882
@@ -91,7 +95,14 @@ def __del__(self):
9195 try :
9296 loop = asyncio .get_event_loop ()
9397 if loop .is_running ():
94- loop .create_task (self .scoped_session .remove ())
98+ task = loop .create_task (self .scoped_session .remove ())
99+ # Add task to the set. This creates a strong reference.
100+ _background_asyncio_tasks .add (task )
101+
102+ # To prevent keeping references to finished tasks forever,
103+ # make each task remove its own reference from the set after
104+ # completion:
105+ task .add_done_callback (_background_asyncio_tasks .discard )
95106 else :
96107 loop .run_until_complete (self .scoped_session .remove ())
97108 except RuntimeError :
You can’t perform that action at this time.
0 commit comments