-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: improve run_sync's python 3.14 compatibility #2006
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
Merged
+196
−5
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e0e57a2
fix: improve run_sync's python 3.14 compatibility
seratch baed3fe
fix
seratch 3537b62
fix review comment
seratch cff7495
fix review comment and add more comments
seratch 6406a2f
fix lint error
seratch 30c2edc
make format
seratch 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import asyncio | ||
| from collections.abc import Generator | ||
|
|
||
| import pytest | ||
|
|
||
| from agents.run import AgentRunner | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fresh_event_loop_policy() -> Generator[asyncio.AbstractEventLoopPolicy, None, None]: | ||
| policy_before = asyncio.get_event_loop_policy() | ||
| new_policy = asyncio.DefaultEventLoopPolicy() | ||
| asyncio.set_event_loop_policy(new_policy) | ||
| try: | ||
| yield new_policy | ||
| finally: | ||
| asyncio.set_event_loop_policy(policy_before) | ||
|
|
||
|
|
||
| def test_run_sync_reuses_existing_default_loop(monkeypatch, fresh_event_loop_policy): | ||
| runner = AgentRunner() | ||
| observed_loops: list[asyncio.AbstractEventLoop] = [] | ||
|
|
||
| async def fake_run(self, *_args, **_kwargs): | ||
| observed_loops.append(asyncio.get_running_loop()) | ||
| return object() | ||
|
|
||
| monkeypatch.setattr(AgentRunner, "run", fake_run, raising=False) | ||
|
|
||
| test_loop = asyncio.new_event_loop() | ||
| fresh_event_loop_policy.set_event_loop(test_loop) | ||
|
|
||
| try: | ||
| runner.run_sync(object(), "input") | ||
| assert observed_loops and observed_loops[0] is test_loop | ||
| finally: | ||
| fresh_event_loop_policy.set_event_loop(None) | ||
| test_loop.close() | ||
|
|
||
|
|
||
| def test_run_sync_creates_default_loop_when_missing(monkeypatch, fresh_event_loop_policy): | ||
| runner = AgentRunner() | ||
| observed_loops: list[asyncio.AbstractEventLoop] = [] | ||
|
|
||
| async def fake_run(self, *_args, **_kwargs): | ||
| observed_loops.append(asyncio.get_running_loop()) | ||
| return object() | ||
|
|
||
| monkeypatch.setattr(AgentRunner, "run", fake_run, raising=False) | ||
|
|
||
| fresh_event_loop_policy.set_event_loop(None) | ||
|
|
||
| runner.run_sync(object(), "input") | ||
| created_loop = observed_loops[0] | ||
| assert created_loop is fresh_event_loop_policy.get_event_loop() | ||
|
|
||
| fresh_event_loop_policy.set_event_loop(None) | ||
| created_loop.close() | ||
|
|
||
|
|
||
| def test_run_sync_errors_when_loop_already_running(monkeypatch, fresh_event_loop_policy): | ||
| runner = AgentRunner() | ||
|
|
||
| async def fake_run(self, *_args, **_kwargs): | ||
| return object() | ||
|
|
||
| monkeypatch.setattr(AgentRunner, "run", fake_run, raising=False) | ||
|
|
||
| async def invoke(): | ||
| with pytest.raises(RuntimeError): | ||
| runner.run_sync(object(), "input") | ||
|
|
||
| asyncio.run(invoke()) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new implementation leaves the coroutine returned by
self.run()attached to the default loop and simply callsdefault_loop.run_until_complete(...)(lines 751‑754). When the loop is interrupted before completion—e.g. a user presses Ctrl+C and triggersKeyboardInterrupt—run_until_completeunwinds immediately and does not cancel the task it created. Because we now keep the loop open for reuse, that unfinished task remains pending on the loop and resumes the next timerun_syncis invoked, so an aborted agent run keeps running concurrently with the next run. This regression is easy to reproduce: startrun_sync, hit Ctrl+C to abort it, then callrun_syncagain and observe the first run continue. The previousasyncio.runcall avoided this by creating a fresh loop per invocation and tearing it down on interruption. Please wrap the scheduled coroutine in a task and cancel/await it whenrun_until_completeexits with an exception (especiallyKeyboardInterrupt) so that aborted runs do not linger on the shared loop.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codex i've resolved this issue. can you review the changes again?