@@ -126,6 +126,7 @@ def use_query(
126126 loading , set_loading = use_state (True )
127127 error , set_error = use_state (cast (Union [Exception , None ], None ))
128128 query_ref = use_ref (query )
129+ async_task_refs = use_ref (set ())
129130 kwargs = kwargs or {}
130131 postprocessor_kwargs = postprocessor_kwargs or {}
131132
@@ -174,7 +175,11 @@ def schedule_query() -> None:
174175 set_should_execute (False )
175176
176177 # Execute the query in the background
177- asyncio .create_task (execute_query ())
178+ task = asyncio .create_task (execute_query ())
179+
180+ # Add the task to a set to prevent it from being garbage collected
181+ async_task_refs .current .add (task )
182+ task .add_done_callback (async_task_refs .current .remove )
178183
179184 @use_callback
180185 def refetch () -> None :
@@ -229,6 +234,7 @@ def use_mutation(
229234
230235 loading , set_loading = use_state (False )
231236 error , set_error = use_state (cast (Union [Exception , None ], None ))
237+ async_task_refs = use_ref (set ())
232238
233239 # The main "running" function for `use_mutation`
234240 async def execute_mutation (exec_args , exec_kwargs ) -> None :
@@ -268,7 +274,11 @@ def schedule_mutation(*exec_args: FuncParams.args, **exec_kwargs: FuncParams.kwa
268274 set_loading (True )
269275
270276 # Execute the mutation in the background
271- asyncio .ensure_future (execute_mutation (exec_args = exec_args , exec_kwargs = exec_kwargs ))
277+ task = asyncio .ensure_future (execute_mutation (exec_args = exec_args , exec_kwargs = exec_kwargs ))
278+
279+ # Add the task to a set to prevent it from being garbage collected
280+ async_task_refs .current .add (task )
281+ task .add_done_callback (async_task_refs .current .remove )
272282
273283 # Used when the user has told us to reset this mutation
274284 @use_callback
0 commit comments