Fix ReAwaitable to support concurrent await calls (#2108)
Fix ReAwaitable to support concurrent await calls
This PR adds thread-safe caching with asyncio.Lock to prevent race conditions when multiple tasks await the same ReAwaitable instance concurrently. The implementation maintains backward compatibility for non-asyncio environments.
Changes made:
- Added thread-safe locking mechanism to ReAwaitable class to handle concurrent awaits
- The lock is lazily initialized on first use to maintain compatibility with non-asyncio environments
- Added comprehensive tests for concurrent await scenarios
- Updated documentation to clarify the thread-safe behavior
Why this change is needed:
Previously, when multiple async tasks awaited the same ReAwaitable instance simultaneously, race conditions could occur leading to the underlying coroutine being executed multiple times. This fix ensures that only one task executes the coroutine while others wait and receive the cached result.
Checklist
- [x] I have double checked that there are no unrelated changes in this pull request
- [x] I have created at least one test case for the changes I have made
- [x] I have updated the documentation for the changes I have made
- [x] I have added my changes to the CHANGELOG.md
Related issues
- Closes #2108
🙏 Please, if you or your company finds dry-python valuable, help us sustain the project by sponsoring it transparently on https://github.com/sponsors/dry-python. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.
Sorry. Working on some missing tests.
This is good to go.
I pray this passes.
I used act to run the GitHub actions locally and found there was a bit more errors that I had to fix. I really hope this passes now.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 100.00%. Comparing base (
82ef3ef) to head (8cb3088). Report is 338 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #2150 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 80 81 +1
Lines 2485 2586 +101
Branches 437 45 -392
==========================================
+ Hits 2485 2586 +101
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
I added one more improvement so that it supports multiple async frameworks including asyncio, trio, and anyio, with automatic framework detection and fallback support.