Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/glider/core/flow_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,10 @@ def on_exec_output(
logger.debug(f"Skipping callback - output '{output_name}' != expected '{expected}'")
return None
logger.debug(f"Exec callback fired: {fn}:{fo} -> {tn}, flow state: {self._state}")
# Execution has left the source node.
self._notify_node_active(fn, False)
# Execution has left the source node -- unless it keeps running
# after this output (a Loop's body); then it clears on return.
if output_name not in getattr(from_node_obj, "STAYS_ACTIVE_ON", ()):
self._notify_node_active(fn, False)
if self._state == FlowState.RUNNING:
logger.info(f"Propagating execution: {fn} -> {tn}")
task = asyncio.create_task(self._propagate_execution(fn, fo, tn))
Expand Down
3 changes: 3 additions & 0 deletions src/glider/nodes/control_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class LoopNode(GliderNode):
],
)

# Still running after firing body: stays lit on the canvas (FlowEngine).
STAYS_ACTIVE_ON = ("body",)

PROPERTIES_SCHEMA = [
{
"key": "count",
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/core/test_flow_node_active.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ async def test_a_node_is_active_from_entry_until_it_fires_on():
# Nothing is left lit once the flow stops.
last = dict(events)
assert not any(last.values())


async def test_a_loop_stays_active_through_its_body_and_waits():
register_experiment_nodes(FlowEngine)
register_control_nodes(FlowEngine)
register_logic_nodes(FlowEngine)
engine = FlowEngine()
engine.initialize()
engine.create_node("start", "StartExperiment", state={})
engine.create_node("loop", "Loop", state={"count": 2, "delay": 0.1})
engine.create_node("delay", "Delay", state={"duration": 0.02})
engine.create_node("end", "EndExperiment", state={})
engine.create_connection("c0", "start", 0, "loop", 0, "exec")
engine.create_connection("c1", "loop", 0, "delay", 0, "exec")
engine.create_connection("c2", "loop", 1, "end", 0, "exec")

events: list[tuple[str, bool]] = []
engine.on_node_active(lambda node_id, active: events.append((node_id, active)))
done = asyncio.Event()
engine.on_flow_complete(done.set)

await engine.start()
await asyncio.wait_for(done.wait(), 5)
await engine.stop()

# Lit once on entry, dark once when done fires: firing body doesn't clear it.
assert [e for e in events if e[0] == "loop"][0] == ("loop", True)
last_body = max(i for i, e in enumerate(events) if e == ("delay", False))
assert events.index(("loop", False)) > last_body
Loading