Skip to content

Commit 84d4027

Browse files
Python: Account for intercepted messages when stopping the runtime (#14656)
### Motivation and Context Dropping a message or raising from an intervention handler can leave `InProcessRuntime.stop_when_idle()` waiting indefinitely. The runtime has removed the envelope from the queue, but its unfinished-task count is never decremented on those early-return paths. ### Description Call `task_done()` before returning from the six send, publish, and response interception branches that drop a message or handle an exception. Normal dispatch retains its existing completion accounting. Add 12 parameterized regression cases covering those three stages with a drop marker class, a drop marker instance, a raised exception, and normal pass-through. ### Validation Baseline: `483ffb487a33a1644886992d56599feae3b69b76`. - Before the fix, runtime/serialization plus the new regression: 9 failed, 23 passed on both Python 3.12.14 and 3.13.15. Each failure reaches the idle-stop deadline. - After the fix, runtime, orchestration, and group-chat suites: 175 passed, 1 skipped on each interpreter. The skip is an existing upstream timing-related skip. - These tests use normal complete-package imports and upstream `tests/conftest.py`. No namespace bootstrap, model API, or external service is involved. - Ruff 0.13.1 lint/format, Python AST, whitespace checks, and clean patch application pass. - The entire repository suite, type checking, and pinned pre-commit hooks have not run. Local command-line network access is blocked, so the repository's Ruff 0.9.6 hook and other missing hook dependencies could not be installed. Command, from `python/`: `python -m pytest tests/unit/agents/runtime tests/unit/agents/orchestration tests/unit/agents/test_group_chat -rs` ### Contribution Checklist - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the complete pre-submission checks pass - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 ### AI assistance OpenAI Codex assisted with source inspection, the regression tests, the six-line fix, and local verification.
1 parent ecb80bd commit 84d4027

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎python/semantic_kernel/agents/runtime/in_process/in_process_runtime.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ async def _process_next(self) -> None:
563563
_warn_if_none(temp_message, "on_send")
564564
except BaseException as e:
565565
future.set_exception(e)
566+
self._message_queue.task_done()
566567
return
567568
if temp_message is DropMessage or isinstance(temp_message, DropMessage):
568569
event_logger.info(
@@ -574,6 +575,7 @@ async def _process_next(self) -> None:
574575
)
575576
)
576577
future.set_exception(MessageDroppedException())
578+
self._message_queue.task_done()
577579
return
578580

579581
message_envelope.message = temp_message
@@ -603,6 +605,7 @@ async def _process_next(self) -> None:
603605
except BaseException as e:
604606
# TODO(evmattso): we should raise the intervention exception to the publisher.
605607
logger.error(f"Exception raised in in intervention handler: {e}", exc_info=True)
608+
self._message_queue.task_done()
606609
return
607610
if temp_message is DropMessage or isinstance(temp_message, DropMessage):
608611
event_logger.info(
@@ -613,6 +616,7 @@ async def _process_next(self) -> None:
613616
kind=MessageKind.PUBLISH,
614617
)
615618
)
619+
self._message_queue.task_done()
616620
return
617621

618622
message_envelope.message = temp_message
@@ -629,6 +633,7 @@ async def _process_next(self) -> None:
629633
except BaseException as e:
630634
# TODO(evmattso): should we raise the exception to sender of the response instead?
631635
future.set_exception(e)
636+
self._message_queue.task_done()
632637
return
633638
if temp_message is DropMessage or isinstance(temp_message, DropMessage):
634639
event_logger.info(
@@ -640,6 +645,7 @@ async def _process_next(self) -> None:
640645
)
641646
)
642647
future.set_exception(MessageDroppedException())
648+
self._message_queue.task_done()
643649
return
644650
message_envelope.message = temp_message
645651
task = asyncio.create_task(self._process_response(message_envelope))
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
from dataclasses import dataclass
5+
6+
import pytest
7+
8+
from semantic_kernel.agents.runtime.core.agent_id import CoreAgentId
9+
from semantic_kernel.agents.runtime.core.exceptions import MessageDroppedException
10+
from semantic_kernel.agents.runtime.core.intervention import DefaultInterventionHandler, DropMessage
11+
from semantic_kernel.agents.runtime.core.message_context import MessageContext
12+
from semantic_kernel.agents.runtime.core.routed_agent import RoutedAgent, message_handler
13+
from semantic_kernel.agents.runtime.in_process.default_topic import DefaultTopicId
14+
from semantic_kernel.agents.runtime.in_process.in_process_runtime import InProcessRuntime
15+
from semantic_kernel.agents.runtime.in_process.type_subscription import TypeSubscription
16+
17+
18+
@dataclass
19+
class Payload:
20+
text: str
21+
22+
23+
class EchoAgent(RoutedAgent):
24+
def __init__(self):
25+
super().__init__("Deterministic echo agent")
26+
27+
@message_handler
28+
async def echo(self, message: Payload, ctx: MessageContext) -> Payload:
29+
return message
30+
31+
32+
class Interceptor(DefaultInterventionHandler):
33+
def __init__(self, stage, outcome):
34+
self.stage = stage
35+
self.outcome = outcome
36+
self.entered = asyncio.Event()
37+
38+
def intercept(self, stage, message):
39+
if stage != self.stage:
40+
return message
41+
self.entered.set()
42+
if self.outcome == "raise":
43+
raise ValueError("intercepted")
44+
if self.outcome == "drop-class":
45+
return DropMessage
46+
if self.outcome == "drop-instance":
47+
return DropMessage()
48+
return message
49+
50+
async def on_send(self, message, *, message_context, recipient):
51+
return self.intercept("send", message)
52+
53+
async def on_publish(self, message, *, message_context):
54+
return self.intercept("publish", message)
55+
56+
async def on_response(self, message, *, sender, recipient):
57+
return self.intercept("response", message)
58+
59+
60+
@pytest.mark.asyncio
61+
@pytest.mark.parametrize("stage", ["send", "publish", "response"])
62+
@pytest.mark.parametrize("outcome", ["drop-class", "drop-instance", "raise", "pass"])
63+
async def test_interception_releases_idle_waiter(stage, outcome):
64+
interceptor = Interceptor(stage, outcome)
65+
runtime = InProcessRuntime(intervention_handlers=[interceptor])
66+
await EchoAgent.register(runtime, "echo", EchoAgent)
67+
await runtime.add_subscription(TypeSubscription("default", "echo"))
68+
runtime.start()
69+
# Retain the run context for cleanup even if a timed-out public stop resets it.
70+
run_context = runtime._run_context
71+
idle_task = None
72+
try:
73+
message = Payload("hello")
74+
if stage == "publish":
75+
await runtime.publish_message(message, DefaultTopicId())
76+
elif outcome == "pass":
77+
assert await asyncio.wait_for(runtime.send_message(message, CoreAgentId("echo", "default")), 1) == message
78+
else:
79+
error = ValueError if outcome == "raise" else MessageDroppedException
80+
with pytest.raises(error):
81+
await asyncio.wait_for(runtime.send_message(message, CoreAgentId("echo", "default")), 1)
82+
83+
await asyncio.wait_for(interceptor.entered.wait(), 1)
84+
# Exercise the public idle-stop API. Shield avoids cancelling the runtime's
85+
# stop coroutine merely because the assertion deadline expires.
86+
idle_task = asyncio.create_task(runtime.stop_when_idle())
87+
await asyncio.wait_for(asyncio.shield(idle_task), 0.5)
88+
finally:
89+
if run_context is not None and not run_context._run_task.done():
90+
await run_context.stop()
91+
if idle_task is not None and not idle_task.done():
92+
idle_task.cancel()
93+
await asyncio.gather(idle_task, return_exceptions=True)
94+
await runtime.close()

0 commit comments

Comments
 (0)