Skip to content

Commit fb628e3

Browse files
authored
fix: move task cancellation to finally block and fix subprocess kill typo (#1234)
1 parent d3d4967 commit fb628e3

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

rdagent/utils/workflow/loop.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,14 @@ async def run(self, step_n: int | None = None, loop_n: int | None = None, all_du
376376
except self.LoopResumeError as e:
377377
logger.warning(f"Stop all the routines and resume loop: {e}")
378378
self.loop_idx = 0
379-
# cancel all previous tasks before resuming all loops.
380-
for t in tasks:
381-
t.cancel()
382379
except self.LoopTerminationError as e:
383380
logger.warning(f"Reach stop criterion and stop loop: {e}")
384381
kill_subprocesses() # NOTE: coroutine-based workflow can't automatically stop subprocesses.
385382
break
386383
finally:
384+
# cancel all previous tasks before resuming all loops or exit
385+
for t in tasks:
386+
t.cancel()
387387
self.close_pbar()
388388

389389
def withdraw_loop(self, loop_idx: int) -> None:
@@ -526,10 +526,12 @@ def kill_subprocesses() -> None:
526526
child.terminate()
527527
except Exception as ex:
528528
print(f"Could not terminate subprocess {child.pid}: {ex}")
529+
print("Finished terminating subprocesses. Then force killing still alive subprocesses.")
529530
_, alive = psutil.wait_procs(current_proc.children(recursive=True), timeout=3)
530531
for p in alive:
531532
try:
532533
print(f"Killing still alive subprocess PID {p.pid} ({p.name()})")
533534
p.kill()
534535
except Exception as ex:
535536
print(f"Could not kill subprocess {p.pid}: {ex}")
537+
print("Finished killing subprocesses.")

0 commit comments

Comments
 (0)