Skip to content

πŸ› Fixed: `AttributeError: '_asyncio.Future' object has no attribute 'handle'` in DependencyInjectorCQRSContainer

Choose a tag to compare

@vadikko2 vadikko2 released this 23 Jan 11:14
· 24 commits to master since this release
d0a5aeb

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: Updated resolve() method to use inspect.isawaitable()

Testing:

  • Added test case test_resolve_async_provider_returns_future to 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 Future objects
  • 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.