@@ -126,6 +126,7 @@ def use_query(
126
126
loading , set_loading = use_state (True )
127
127
error , set_error = use_state (cast (Union [Exception , None ], None ))
128
128
query_ref = use_ref (query )
129
+ async_task_refs = use_ref (set ())
129
130
kwargs = kwargs or {}
130
131
postprocessor_kwargs = postprocessor_kwargs or {}
131
132
@@ -174,7 +175,11 @@ def schedule_query() -> None:
174
175
set_should_execute (False )
175
176
176
177
# 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 )
178
183
179
184
@use_callback
180
185
def refetch () -> None :
@@ -229,6 +234,7 @@ def use_mutation(
229
234
230
235
loading , set_loading = use_state (False )
231
236
error , set_error = use_state (cast (Union [Exception , None ], None ))
237
+ async_task_refs = use_ref (set ())
232
238
233
239
# The main "running" function for `use_mutation`
234
240
async def execute_mutation (exec_args , exec_kwargs ) -> None :
@@ -268,7 +274,11 @@ def schedule_mutation(*exec_args: FuncParams.args, **exec_kwargs: FuncParams.kwa
268
274
set_loading (True )
269
275
270
276
# 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 )
272
282
273
283
# Used when the user has told us to reset this mutation
274
284
@use_callback
0 commit comments