Skip to content

Conversation

@strands-agent
Copy link
Contributor

Description

Fixes flaky concurrent invocation tests that were failing intermittently on macOS Python 3.13.

Root Cause

The tests test_agent_concurrent_call_raises_exception and test_agent_concurrent_structured_output_raises_exception used timing-based synchronization (time.sleep(0.05)) to ensure the second thread started while the first thread held the invocation lock. On faster machines (particularly macOS Python 3.13), thread scheduling could allow the first invocation to complete before the second thread started, causing both threads to succeed instead of one raising ConcurrencyException.

Solution

Replace timing-based synchronization with proper thread coordination using threading.Event:

  1. Extended SlowMockedModel to accept an optional signal_event parameter
  2. The model signals when it starts streaming (at which point the lock is held)
  3. Tests now use lock_acquired.wait(timeout=2.0) instead of time.sleep(0.05)
  4. This ensures deterministic test behavior regardless of machine speed

Changes

  • SlowMockedModel now accepts delay and signal_event parameters
  • Tests wait for explicit signal rather than arbitrary sleep duration
  • Renamed local lock variable to results_lock for clarity

Related Issues

This flaky test was blocking PR #1491 and potentially other PRs with spurious CI failures.

Type of Change

Bug fix (test flakiness)

Testing

  • Ran the concurrent tests 5+ times locally, all passed consistently
  • hatch fmt passes

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests that prove my fix is effective or that my feature works
  • My changes generate no new warnings

Replace timing-based synchronization (time.sleep) with threading.Event
to ensure the second thread always starts while the first thread holds
the invocation lock.

Changes:
- Extended SlowMockedModel to accept optional signal_event parameter
- Tests now use lock_acquired.wait() instead of time.sleep(0.05)
- Renamed local 'lock' variable to 'results_lock' for clarity

This fixes flaky test failures observed on macOS Python 3.13 where
thread scheduling could allow the first invocation to complete before
the second thread started, causing both threads to succeed instead of
one raising ConcurrencyException.
@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant