returns icon indicating copy to clipboard operation
returns copied to clipboard

Fix ReAwaitable to support concurrent await calls (#2108)

Open AdrianAcala opened this issue 8 months ago • 6 comments

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.

AdrianAcala avatar May 30 '25 07:05 AdrianAcala

Sorry. Working on some missing tests.

AdrianAcala avatar May 30 '25 19:05 AdrianAcala

This is good to go.

AdrianAcala avatar May 30 '25 20:05 AdrianAcala

I pray this passes.

AdrianAcala avatar May 31 '25 01:05 AdrianAcala

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.

AdrianAcala avatar May 31 '25 20:05 AdrianAcala

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.

codecov[bot] avatar May 31 '25 20:05 codecov[bot]

I added one more improvement so that it supports multiple async frameworks including asyncio, trio, and anyio, with automatic framework detection and fallback support.

AdrianAcala avatar May 31 '25 23:05 AdrianAcala