Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detected blocking call inside the event loop #1120

Closed
wants to merge 1 commit into from

Conversation

chemelli74
Copy link

Description of Change

Fix detected blocking call inside the event loop. Reference HomeAssistant latest build:

2024-06-12 09:40:41.237 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to listdir inside the event loop by integration 'aws'
at homeassistant/components/aws/notify.py, line 38: return await session.get_available_regions(service) (offender: /usr/local/lib/python3.12/site-pa
ckages/botocore/loaders.py, line 311: api_versions = os.listdir(full_dirname)), please create a bug report at https://github.com/home-assistant/core
/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+aws%22

Assumptions

none

Checklist for All Submissions

  • I have added change info to CHANGES.rst
  • If this is resolving an issue (needed so future developers can determine if change is still necessary and under what conditions) (can be provided via link to issue with these details):
    • Detailed description of issue
    • Alternative methods considered (if any)
    • How issue is being resolved
    • How issue can be reproduced
  • If this is providing a new feature (can be provided via link to issue with these details):
    • Detailed description of new feature
    • Why needed
    • Alternatives methods considered (if any)

Checklist when updating botocore and/or aiohttp versions

  • I have read and followed CONTRIBUTING.rst
  • I have updated test_patches.py where/if appropriate (also check if no changes necessary)
  • I have ensured that the awscli/boto3 versions match the updated botocore version

@thehesiod
Copy link
Collaborator

sessions should be cached for the life of use so if this is a major issue the session should be kept around for a longer time. really botocore needs to speed this up on their end because it's way too slow

@chemelli74
Copy link
Author

Until then we have a fix anyway.
Maybe we add a comment about that.

@thehesiod
Copy link
Collaborator

I'm hesitant of doing this as run_in_executor triggers a thread pool to be created (if you haven't called it before) which can cause overhead. Further the executor should be configurable..this really opens a can of worms.

@bdraco
Copy link
Member

bdraco commented Jun 14, 2024

It does looks like load_service_model calls are cached, but it does do blocking I/O in the event loop the first time its created. Ideally we would only call it in the executor the first time it needs to be created.

run_in_executor being called with None (default executor) seems fine to me as we do it all over the place in aiohttp and that is the prescribed way to run blocking code https://docs.python.org/3/library/asyncio-dev.html#running-blocking-code

@chemelli74
Copy link
Author

Any feedback @thehesiod ? Thank you in advance

@thehesiod
Copy link
Collaborator

so this seems like an issue with the aws component, why doesn't it do the session/client creation during async_setup instead? That seems like the correct fix no?

@thehesiod
Copy link
Collaborator

like i was saying, it should cache the session/client there, or if you don't want to hold onto the client, perhaps just pre-warm up the botocore cache

@thehesiod
Copy link
Collaborator

actually, this is invalid as botocore is not thread safe, with this you could have two requests, running in two different threads, trying to initialize the cache at the same time. closing

@thehesiod thehesiod closed this Jun 25, 2024
@bdraco
Copy link
Member

bdraco commented Jun 25, 2024

@chemelli74 Thanks for trying to fix this. This problem must be solved eventually (unless the intent is WONTFIX). Please open an issue instead to track this in the long term. Thanks.

@thehesiod
Copy link
Collaborator

ya let me know if I missed something, I truly believe this is a botocore issue. The fact they're reading a ton of JSON files is unsustainable. probably should be done on the fly per service when you use it...and even then should be in a post-processed state, like their build should generate python code based on those json files.

@chemelli74 chemelli74 deleted the chemelli74-fix-io branch June 25, 2024 07:11
@thehesiod
Copy link
Collaborator

so I looked a little into this, it seems like they use an instance_cache which basically just sets an attribute on the class, so presumably worst case scenario with this fix is you'd initialize the cache multiple times. However I really want to avoid adding threads to this library if possible. Looks like there are some async file alternatives like https://github.com/qweeze/uring_file for debian, I'd be open to something like that.

@chemelli74
Copy link
Author

What about pathlib ?
Is used all over

@thehesiod
Copy link
Collaborator

that uses aiofiles which just uses a threadpool again: https://github.com/Tinche/aiofiles/tree/main/src/aiofiles

@thehesiod
Copy link
Collaborator

i guess at the aiofiles level though it doesn't matter. But it sounds like there's a better solution

@chemelli74
Copy link
Author

chemelli74 commented Jul 17, 2024

i guess at the aiofiles level though it doesn't matter. But it sounds like there's a better solution

If you point me towards the solution you prefer, I'll try to work on it.
I really would like to fix this issue and it's effect on Home Assistant.

EDIT: Opened issue on botocore repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants