fix: avoid triggering errors like "RuntimeError: dictionary changed s… (#1285)

* fix: avoid triggering errors like "RuntimeError: dictionary changed size during iteration"

* style: reformat run_in_executor call for improved readability
This commit is contained in:
you-n-g
2025-11-08 22:55:29 +08:00
committed by GitHub
parent 8c1523802c
commit b18054371c
+6 -1
View File
@@ -10,6 +10,7 @@ Postscripts:
import asyncio
import concurrent.futures
import copy
import os
import pickle
from collections import defaultdict
@@ -227,7 +228,11 @@ class LoopBase:
if force_subproc:
curr_loop = asyncio.get_running_loop()
with concurrent.futures.ProcessPoolExecutor() as pool:
result = await curr_loop.run_in_executor(pool, func, self.loop_prev_out[li])
# Using deepcopy is to avoid triggering errors like "RuntimeError: dictionary changed size during iteration"
# GUESS: Some content in self.loop_prev_out[li] may be in the middle of being changed.
result = await curr_loop.run_in_executor(
pool, copy.deepcopy(func), copy.deepcopy(self.loop_prev_out[li])
)
else:
# auto determine whether to run async or sync
if asyncio.iscoroutinefunction(func):