Skip to content

Conversation

@Rakshit-gen
Copy link

@Rakshit-gen Rakshit-gen commented Dec 20, 2025

Fixes #4135

  • Replace context manager with explicit try/finally block
  • Check cancel() return value and handle both success/failure cases
  • Always call executor.shutdown(wait=True) to ensure thread cleanup
  • Prevents resource leaks and thread accumulation under load

Fixes issue where tasks timing out would create orphaned threads that hold resources indefinitely, leading to memory leaks and potential crashes in production environments.


Note

Refactors Agent._execute_with_timeout to robustly cancel/wait on futures and always shutdown the ThreadPoolExecutor, preventing orphaned threads and resource leaks.

  • Agent execution (sync timeout path):
    • Replace context manager with explicit ThreadPoolExecutor and finally-ensured executor.shutdown(wait=True).
    • On timeout and other exceptions, check future.cancel(); if already running, wait briefly on future.result(...) to ensure thread cleanup.
    • Preserve behavior by raising TimeoutError/RuntimeError while preventing orphaned threads.

Written by Cursor Bugbot for commit e18234d. This will update automatically on new commits. Configure here.

- Replace context manager with explicit try/finally block
- Check cancel() return value and handle both success/failure cases
- Always call executor.shutdown(wait=True) to ensure thread cleanup
- Prevents resource leaks and thread accumulation under load

Fixes issue where tasks timing out would create orphaned threads
that hold resources indefinitely, leading to memory leaks and
potential crashes in production environments.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

When future.result() is called during cleanup, it can re-raise task
exceptions. The previous code only caught TimeoutError and CancelledError,
allowing other exceptions to leak through and break the API contract.

Now catch all exceptions during cleanup to ensure:
- TimeoutError is always raised for timeouts (not task exceptions)
- RuntimeError is always raised for task failures (not original exceptions)

This maintains the documented API contract while still ensuring proper
thread cleanup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant