-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(approval): prevent parallel subagent approval hang in interactive shell #1724
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,9 @@ def create_request( | |
| self._publish_wire_request(request) | ||
| return request | ||
|
|
||
| async def wait_for_response(self, request_id: str) -> tuple[ApprovalResponseKind, str]: | ||
| async def wait_for_response( | ||
| self, request_id: str, timeout: float = 300.0 | ||
| ) -> tuple[ApprovalResponseKind, str]: | ||
| waiter = self._waiters.get(request_id) | ||
| request = self._requests.get(request_id) | ||
| if request is None: | ||
|
|
@@ -95,7 +97,20 @@ async def wait_for_response(self, request_id: str) -> tuple[ApprovalResponseKind | |
| return request.response, request.feedback | ||
| waiter = asyncio.get_running_loop().create_future() | ||
| self._waiters[request_id] = waiter | ||
| return await waiter | ||
| try: | ||
| return await asyncio.wait_for(asyncio.shield(waiter), timeout=timeout) | ||
| except TimeoutError: | ||
| logger.warning( | ||
| "Approval request {id} timed out after {t}s", | ||
| id=request_id, | ||
| t=timeout, | ||
| ) | ||
| # Pop the waiter before cancelling so _cancel_request won't | ||
| # set_exception on a future that nobody is awaiting (which would | ||
| # trigger an "exception was never retrieved" warning from asyncio). | ||
| self._waiters.pop(request_id, None) | ||
| self._cancel_request(request_id, feedback="approval timed out") | ||
| raise ApprovalCancelledError(request_id) from None | ||
|
Comment on lines
+100
to
+113
|
||
|
|
||
| def resolve(self, request_id: str, response: ApprovalResponseKind, feedback: str = "") -> bool: | ||
| request = self._requests.get(request_id) | ||
|
|
@@ -114,6 +129,23 @@ def resolve(self, request_id: str, response: ApprovalResponseKind, feedback: str | |
| self._publish_wire_response(request_id, response, feedback) | ||
| return True | ||
|
|
||
| def _cancel_request(self, request_id: str, feedback: str = "") -> None: | ||
| """Cancel a single pending request by ID.""" | ||
| import time | ||
|
|
||
| request = self._requests.get(request_id) | ||
| if request is None or request.status != "pending": | ||
| return | ||
| request.status = "cancelled" | ||
| request.response = "reject" | ||
| request.feedback = feedback | ||
| request.resolved_at = time.time() | ||
| waiter = self._waiters.pop(request_id, None) | ||
| if waiter is not None and not waiter.done(): | ||
| waiter.set_exception(ApprovalCancelledError(request_id)) | ||
| self._publish_event(ApprovalRuntimeEvent(kind="request_resolved", request=request)) | ||
| self._publish_wire_response(request_id, "reject", feedback) | ||
|
|
||
| def cancel_by_source(self, source_kind: ApprovalSourceKind, source_id: str) -> int: | ||
| cancelled = 0 | ||
| import time | ||
|
|
||
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.
In the timeout handler, popping the waiter before calling
_cancel_request()means the shared Future is never completed (noset_exception/set_result). If another task is also awaiting the samewaiter(e.g. concurrent callers ofwait_for_responsefor the same request), it won’t be released when the request is cancelled and will block until its own timeout. Consider cancelling via_cancel_request()without removing the waiter first, and suppressing the “exception was never retrieved” warning by explicitly retrieving the exception (or adding a done-callback that callsfuture.exception()).