-
Notifications
You must be signed in to change notification settings - Fork 887
FIX Harden prompt target cancellation cleanup #2483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Roman Lutz (romanlutz)
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
romanlutz:romanlutz-cancellation-hardening
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−34
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d6af47
Harden target cancellation cleanup
romanlutz 896cb99
Address realtime cleanup review feedback
romanlutz 9f3f46e
Merge origin/main into cancellation hardening
romanlutz 1f70d88
Merge branch 'main' into romanlutz-cancellation-hardening
romanlutz 5ec0bdd
Merge branch 'main' into romanlutz-cancellation-hardening
romanlutz cdb773a
Merge remote-tracking branch 'origin/main' into romanlutz-cancellatio…
romanlutz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,6 +86,71 @@ async def test_send_prompt_async(target): | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| await target.cleanup_target_async() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_cancellation_during_session_config_discards_connection(target): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection = AsyncMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target._connect_async = AsyncMock(return_value=connection) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config_started = asyncio.Event() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def wait_in_config_async(*, conversation_id: str, conversation: list[Message]) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config_started.set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await asyncio.Event().wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target.send_config_async = AsyncMock(side_effect=wait_in_config_async) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message = Message.from_prompt(prompt="Hello", role="user") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message.get_piece().conversation_id = "cancelled-config" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| send_task = asyncio.create_task(target.send_prompt_async(message=message)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await config_started.wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| send_task.cancel() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with pytest.raises(asyncio.CancelledError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await send_task | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection.close.assert_awaited_once_with() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "cancelled-config" not in target._existing_conversation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_response_create_failure_cancels_receive_task(target): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connection = AsyncMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target._existing_conversation["response-failure"] = connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| receive_started = asyncio.Event() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| receive_cancelled = asyncio.Event() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def receive_events_async(*, conversation_id: str) -> RealtimeTargetResult: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| receive_started.set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await asyncio.Event().wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| receive_cancelled.set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise AssertionError("unreachable") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def fail_response_create_async(*, conversation_id: str) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await receive_started.wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("response create failed") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target.receive_events_async = receive_events_async | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target.send_response_create_async = AsyncMock(side_effect=fail_response_create_async) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with pytest.raises(RuntimeError, match="response create failed"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await target.send_text_async(text="Hello", conversation_id="response-failure") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert receive_started.is_set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert receive_cancelled.is_set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_cancel_receive_task_async_gathers_completed_failure(target): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def fail_receive_async() -> RealtimeTargetResult: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise RuntimeError("receive failed") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| receive_task = asyncio.create_task(fail_receive_async()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with pytest.raises(RuntimeError, match="receive failed"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await receive_task | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with patch.object(asyncio, "gather", new_callable=AsyncMock) as gather: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await target._cancel_receive_task_async(receive_task=receive_task) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gather.assert_awaited_once_with(receive_task, return_exceptions=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+140
to
+151
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should avoid awaiting the task or mocking
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_send_prompt_async_propagates_interrupted_to_metadata(target): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """When a turn result carries interrupted=True, both response pieces' metadata must reflect it.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target._connect_async = AsyncMock(return_value=AsyncMock()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1433,6 +1498,34 @@ async def test_reset_conversation_async_swallows_close_error(target): | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "conv" not in target._existing_conversation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_reset_conversation_async_finishes_close_before_propagating_cancellation(target): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close_started = asyncio.Event() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close_release = asyncio.Event() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close_finished = asyncio.Event() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def close_async() -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close_started.set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await close_release.wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close_finished.set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_connection = AsyncMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_connection.close.side_effect = close_async | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| target._existing_conversation["conv"] = mock_connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleanup_task = asyncio.create_task(target.reset_conversation_async(conversation_id="conv")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await close_started.wait() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cleanup_task.cancel() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await asyncio.sleep(0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert not cleanup_task.done() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close_release.set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with pytest.raises(asyncio.CancelledError): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await cleanup_task | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert close_finished.is_set() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "conv" not in target._existing_conversation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_reset_conversation_async_propagates_cancellation(target): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_connection = AsyncMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mock_connection.close.side_effect = asyncio.CancelledError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.