π Fixed: `AttributeError: '_asyncio.Future' object has no attribute 'handle'` in DependencyInjectorCQRSContainer
Release v4.6.2
π Bug Fixes
Fixed: AttributeError: '_asyncio.Future' object has no attribute 'handle' in DependencyInjectorCQRSContainer
Problem:
When using DependencyInjectorCQRSContainer with async providers or providers that return Future objects, the container's resolve() method was not properly awaiting these awaitable objects. This caused Future objects to be returned directly instead of the resolved handler instances, leading to the error:
AttributeError: '_asyncio.Future' object has no attribute 'handle'
Root Cause:
The code was using inspect.iscoroutine() to check if a provider result needed to be awaited. However, iscoroutine() only returns True for coroutine objects (results of calling async def functions), not for Future or Task objects. When dependency-injector providers returned Future objects (which can happen with async providers or certain provider configurations), the check failed and the Future was returned directly.
Solution:
Replaced inspect.iscoroutine(result) with inspect.isawaitable(result) in DependencyInjectorCQRSContainer.resolve(). The isawaitable() function correctly identifies all awaitable objects including:
- Coroutines
- Futures
- Tasks
- Any object with
__await__method
Impact:
- β
Fixes the error when using async providers with
DependencyInjectorCQRSContainer - β Properly handles all types of awaitable objects returned by providers
- β No breaking changes - fully backward compatible
- β
Works correctly with
bootstrap_streaming()and parallel event processing
Files Changed:
src/cqrs/container/dependency_injector.py: Updatedresolve()method to useinspect.isawaitable()
Testing:
- Added test case
test_resolve_async_provider_returns_futureto validate async provider resolution - All existing tests continue to pass
Related Issues:
This fix resolves issues when using StreamingRequestMediator with DependencyInjectorCQRSContainer in scenarios where:
- Providers return
Futureobjects - Async providers are used
- Parallel event processing is enabled (
concurrent_event_handle_enable=True)
π Notes
This is a patch release that fixes a critical bug affecting users of DependencyInjectorCQRSContainer with async providers. The fix is backward compatible and does not introduce any breaking changes.
Upgrade Recommendation:
All users experiencing the '_asyncio.Future' object has no attribute 'handle' error should upgrade to this version.