Skip to content

Commit 39e9d31

Browse files
committed
Wait for cancellation
1 parent 45185b3 commit 39e9d31

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

temporalio/contrib/openai_agents/_mcp.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
TIMEOUT_TYPE_HEARTBEAT,
1818
TIMEOUT_TYPE_SCHEDULE_TO_START,
1919
)
20-
from temporalio.exceptions import ActivityError, ApplicationError
20+
from temporalio.exceptions import (
21+
ActivityError,
22+
ApplicationError,
23+
CancelledError,
24+
is_cancelled_exception,
25+
)
2126
from temporalio.worker import PollerBehaviorSimpleMaximum, Worker
2227
from temporalio.workflow import ActivityConfig, ActivityHandle
2328

@@ -213,6 +218,13 @@ async def connect(self) -> None:
213218
async def cleanup(self) -> None:
214219
if self._connect_handle:
215220
self._connect_handle.cancel()
221+
try:
222+
await self._connect_handle
223+
except Exception as e:
224+
if is_cancelled_exception(e):
225+
pass
226+
else:
227+
raise
216228

217229
async def __aenter__(self):
218230
await self.connect()

tests/contrib/openai_agents/test_openai.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Optional,
1313
Sequence,
1414
Union,
15+
cast,
1516
no_type_check,
1617
)
1718

@@ -2466,6 +2467,7 @@ async def get_prompt(
24662467
"list_tools",
24672468
"cleanup",
24682469
]
2470+
assert len(cast(StatefulMCPServerProvider, server)._servers) == 0
24692471
else:
24702472
assert tracking_server.calls == [
24712473
"connect",

0 commit comments

Comments
 (0)