---
type: email
from: Copilot <notifications@github.com>
subject: Re: [openai/openai-agents-python] Fix: Properly await cancelled

 guardrail tasks during cleanup (PR #1976)
received: 2026-03-06 08:13:37
date_header: Wed, 22 Oct 2025 09:37:46 -0700
priority: normal
status: awaiting_approval
uid: 33462
---

## Email Content

**From:** Copilot <notifications@github.com>
**Subject:** Re: [openai/openai-agents-python] Fix: Properly await cancelled

 guardrail tasks during cleanup (PR #1976)
**Date:** Wed, 22 Oct 2025 09:37:46 -0700

### Body (preview)

@Copilot commented on this pull request.



## Pull Request Overview



This PR fixes a resource leak in guardrail task cleanup by ensuring cancelled tasks are properly awaited during session shutdown. The fix prevents unhandled task exception warnings and follows Python asyncio best practices.



**Key changes:**

- Converted `_cleanup_guardrail_tasks()` from sync to async method to properly await cancelled tasks

- Added `asyncio.gather()` with `return_exceptions=True` to collect exceptions from cancelled tasks

- Implemented comprehensive test coverage for the cleanup behavior



### Reviewed Changes



Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.



| File | Description |

| ---- | ----------- |

| src/agents/realtime/session.py | Made `_cleanup_guardrail_tasks()` async and added proper task awaiting with exception handling |

| tests/realtime/test_guardrail_cleanup.py | Added comprehensive tests verifying proper cleanup of cancelled, failed, and multiple concurrent guardrail tasks |

| src/agents/agent.py | Added validation to ensure tools list contains only valid Tool objects, preventing runtime AttributeErrors |

| tests/test_agent_config.py | Added test coverage for tool validation to prevent invalid tool types from passing initialization |











---



<sub>**Tip:** Customize your code reviews with copilot-instructions.md. <a href="/openai/openai-agents-python/new/main/.github?filename=copilot-instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Create the file</a> or <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">learn how to get started</a>.</sub>



> @@ -246,6 +247,21 @@ def __post_init__(self):

         if not isinstance(self.tools, list):

             raise TypeError(f"Agent tools must be a list, got {type(self.tools).__name__}")

---

## Suggested Actions
- [ ] Reply
- [ ] Archive
- [ ] Escalate
