1. TypingIndicator Concurrency & Argument fixes#187
Conversation
Problems Fixed: ❌ Used threading.Timer (synchronous) in async code → Event loop blocking ❌ Timer couldn't properly execute async callbacks ❌ No race condition protection for concurrent start() calls ❌ Incorrect time unit handling (Timer uses seconds, code expected milliseconds) ❌ Complex and error-prone callback pattern Solutions Implemented: ✅ Replaced threading.Timer with asyncio.Task for proper async operation ✅ Created continuous _typing_loop() that runs asynchronously ✅ Added _running flag to prevent race conditions ✅ Proper interval conversion (milliseconds to seconds) ✅ Clean async/await pattern throughout ✅ Graceful cancellation handling with asyncio.CancelledError ✅ Made stop() async to properly await task cancellation 2. AgentApplication Class (agent_application.py) Problems Fixed: ❌ Called self.typing.stop() without await (sync call to async method) Solutions Implemented: ✅ Changed to await self.typing.stop() for proper async execution 📋 Key Improvements: No Thread Blocking: Uses asyncio tasks instead of threads, preventing event loop blocking Proper Async Patterns: All methods properly use async/await Race Condition Protection: The _running flag prevents multiple simultaneous starts Clean Shutdown: Proper task cancellation and cleanup Better Error Handling: Catches and logs exceptions without crashing Correct Timing: Proper conversion from milliseconds to seconds for asyncio.sleep() 🎯 Benefits: Performance: No blocking of the async event loop Reliability: No deadlocks or race conditions Maintainability: Cleaner, more Pythonic async code Scalability: Works correctly with concurrent operations Safety: Proper resource cleanup and cancellation handling Both files now follow best practices for async Python code and are free from threading bugs!
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the TypingIndicator class to use proper async/await patterns with asyncio.Task instead of threading.Timer, eliminating event loop blocking and race conditions in async code.
Key Changes:
- Replaced synchronous threading-based timer with asynchronous task-based implementation
- Added race condition protection with
_runningflag - Fixed time unit handling and async method calls
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| typing_indicator.py | Converted from threading.Timer to asyncio.Task with continuous typing loop, proper async patterns, and race condition protection |
| agent_application.py | Updated to await the now-async stop() method |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
rodrigobr-msft
left a comment
There was a problem hiding this comment.
TypingIndicator needs a broader refactor.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
…ub.com/Microsoft/Agents-for-python into users/cleemullins/TypingIndicatorFixes
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
…into users/cleemullins/TypingIndicatorFixes
…ub.com/microsoft/Agents-for-python into users/cleemullins/TypingIndicatorFixes
* Fastapi integration draft * Potential fix for code scanning alert no. 11: Information exposure through an exception Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * Update libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/app/streaming/streaming_response.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fastapi integration wip * Fastapi integration wip, emtpy agent working * Fastapi integration wip, auth agent bugfixing * Revert "1. TypingIndicator Concurrency & Argument fixes (#187)" This reverts commit 3024b7c. * Fastapi integration tentative, need to validate auth behavior * Fastapi integration tentative, need to validate auth behavior: nit fix * Reapply "1. TypingIndicator Concurrency & Argument fixes (#187)" This reverts commit e7f0cad. * Update test_samples/fastapi/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test_samples/fastapi/shared/__init__.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test_samples/fastapi/shared/github_api_client.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test_samples/fastapi/authorization_agent.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fastapi integration tentative, need to validate auth behavior: copilot rreview fixers * Update test_samples/fastapi/shared/github_api_client.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update libraries/microsoft-agents-hosting-fastapi/microsoft_agents/hosting/fastapi/app/streaming/streaming_response.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update test_samples/fastapi/shared/cards.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fastapi integration tentative, need to validate auth behavior: nit fix --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Fixes #186
Problems Fixed:
❌ Used threading.Timer (synchronous) in async code → Event loop blocking
❌ Timer couldn't properly execute async callbacks
❌ No race condition protection for concurrent start() calls
❌ Incorrect time unit handling (Timer uses seconds, code expected milliseconds)
❌ Complex and error-prone callback pattern
Solutions Implemented:
✅ Replaced threading.Timer with asyncio.Task for proper async operation
✅ Created continuous _typing_loop() that runs asynchronously
✅ Added _running flag to prevent race conditions
✅ Proper interval conversion (milliseconds to seconds)
✅ Clean async/await pattern throughout
✅ Graceful cancellation handling with asyncio.CancelledError
✅ Made stop() async to properly await task cancellation
2. AgentApplication Class (agent_application.py)
Problems Fixed:
❌ Called self.typing.stop() without await (sync call to async method)
Solutions Implemented:
✅ Changed to await self.typing.stop() for proper async execution
📋 Key Improvements:
No Thread Blocking: Uses asyncio tasks instead of threads, preventing event loop blocking
Proper Async Patterns: All methods properly use async/await
Race Condition Protection: The _running flag prevents multiple simultaneous starts
Clean Shutdown: Proper task cancellation and cleanup
Better Error Handling: Catches and logs exceptions without crashing
Correct Timing: Proper conversion from milliseconds to seconds for asyncio.sleep()
🎯 Benefits:
Performance: No blocking of the async event loop
Reliability: No deadlocks or race conditions
Maintainability: Cleaner, more Pythonic async code
Scalability: Works correctly with concurrent operations
Safety: Proper resource cleanup and cancellation handling
Both files now follow best practices for async Python code and are free from threading bugs!