Skip to content

Comments

1. TypingIndicator Concurrency & Argument fixes#187

Merged
cleemullins merged 13 commits intomainfrom
users/cleemullins/TypingIndicatorFixes
Oct 21, 2025
Merged

1. TypingIndicator Concurrency & Argument fixes#187
cleemullins merged 13 commits intomainfrom
users/cleemullins/TypingIndicatorFixes

Conversation

@cleemullins
Copy link
Collaborator

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!

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!
@cleemullins cleemullins requested a review from a team as a code owner October 20, 2025 22:34
Copilot AI review requested due to automatic review settings October 20, 2025 22:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _running flag
  • 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.

@cleemullins cleemullins changed the title 1. TypingIndicator Class (typing_indicator.py) 1. TypingIndicator Concurrency & Argument fixes Oct 20, 2025
@cleemullins cleemullins self-assigned this Oct 20, 2025
@cleemullins cleemullins added the Bug Something isn't working label Oct 20, 2025
Copilot AI review requested due to automatic review settings October 20, 2025 22:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@rodrigobr-msft rodrigobr-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypingIndicator needs a broader refactor.

Copilot AI review requested due to automatic review settings October 21, 2025 20:58
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI review requested due to automatic review settings October 21, 2025 21:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI review requested due to automatic review settings October 21, 2025 21:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cleemullins cleemullins enabled auto-merge (squash) October 21, 2025 21:27
@rodrigobr-msft rodrigobr-msft linked an issue Oct 21, 2025 that may be closed by this pull request
@rodrigobr-msft rodrigobr-msft self-requested a review October 21, 2025 21:50
@cleemullins cleemullins merged commit 3024b7c into main Oct 21, 2025
10 checks passed
@cleemullins cleemullins deleted the users/cleemullins/TypingIndicatorFixes branch October 21, 2025 21:53
axelsrz added a commit that referenced this pull request Oct 24, 2025
axelsrz added a commit that referenced this pull request Oct 27, 2025
axelsrz added a commit that referenced this pull request Oct 27, 2025
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor TypingIndicator to create a unique instance per turn TypingIndicator Uses threading.Timer with Async Callable

3 participants