Skip to content

Commit 1daeb29

Browse files
authored
fix: 🐛 Fix remove task functionality on _execute_task, specially when the task has gone missing (#200)
1 parent babb1ab commit 1daeb29

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/workflows/runtime/broker.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
instrument_tags,
2525
)
2626

27-
28-
from workflows.utils import _nanoid as nanoid
2927
from workflows.errors import WorkflowRuntimeError
3028
from workflows.events import (
3129
Event,
3230
StartEvent,
3331
)
32+
from workflows.handler import WorkflowHandler
3433
from workflows.runtime.control_loop import control_loop, rebuild_state_from_ticks
3534
from workflows.runtime.types.internal_state import BrokerState
3635
from workflows.runtime.types.plugin import Plugin, WorkflowRuntime, as_snapshottable
@@ -49,11 +48,10 @@
4948
)
5049
from workflows.runtime.types.ticks import TickAddEvent, TickCancelRun, WorkflowTick
5150
from workflows.runtime.workflow_registry import workflow_registry
51+
from workflows.utils import _nanoid as nanoid
5252

5353
from ..context.state_store import MODEL_T
5454

55-
from workflows.handler import WorkflowHandler
56-
5755
if TYPE_CHECKING:
5856
from workflows import Workflow
5957
from workflows.context.context import Context
@@ -105,7 +103,15 @@ def __init__(
105103
def _execute_task(self, coro: Coroutine[Any, Any, Any]) -> asyncio.Task[Any]:
106104
task = asyncio.create_task(coro)
107105
self._workers.append(task)
108-
task.add_done_callback(lambda _: self._workers.remove(task))
106+
107+
def _remove_task(_: asyncio.Task[Any]) -> None:
108+
try:
109+
self._workers.remove(task)
110+
except ValueError:
111+
# Handle Task was already cleared during shutdown or cleanup.
112+
pass
113+
114+
task.add_done_callback(_remove_task)
109115
return task
110116

111117
# context API only

0 commit comments

Comments
 (0)