-
Notifications
You must be signed in to change notification settings - Fork 165
fix: cloud provider detection with running event loop #7882
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
Conversation
Avoid calling asyncio.run() when an event loop is already running to prevent runtime errors. Detection is skipped at module load if a loop is active, setting current_provider to None.
.fix.md -> 7882.fix.md Co-authored-by: octodog <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a runtime error that occurs when the identity module is loaded while an asyncio event loop is already running. The fix prevents calling asyncio.run() from within a running event loop by checking for an active loop and skipping cloud provider detection in that case.
Key Changes
- Modified cloud provider detection logic to check for a running event loop before attempting to call
asyncio.run() - Sets
current_providertoNonewhen a running loop is detected, deferring cloud detection to avoid runtime errors
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| asyncio.get_running_loop() | ||
| # If there's already a running loop, skip detection at module load | ||
| # to avoid "cannot be called from a running event loop" error | ||
| current_provider = None |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logging message "Detected environment: on-premise setup" is misleading when current_provider is None due to a running event loop. In this case, the cloud provider detection was skipped (not performed), not that an on-premise environment was detected. Consider adding a separate log message to distinguish between "detection skipped due to running event loop" and "detected on-premise setup".
| current_provider = loop.run_until_complete(detect_cloud()) | ||
| asyncio.get_running_loop() | ||
| # If there's already a running loop, skip detection at module load | ||
| # to avoid "cannot be called from a running event loop" error |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a running event loop is detected and current_provider is set to None, the subsequent call to _define_functions() (line 341) will configure the module to behave as if it's running in an on-premise environment. This means that cloud-specific functionality (like instance ID detection) will fall back to hostname-based behavior even if the code is actually running in a cloud environment. Consider documenting this behavior or providing an alternative mechanism to detect the cloud provider after module initialization for cases where the module is loaded within a running event loop.
| # to avoid "cannot be called from a running event loop" error | |
| # to avoid "cannot be called from a running event loop" errors. | |
| # In this case, current_provider remains None and will be treated as | |
| # an on-premise environment by downstream logic. If accurate cloud | |
| # detection is required in such environments, callers should invoke | |
| # detect_cloud() from within the running event loop and update | |
| # current_provider accordingly. | |
| log.info( | |
| "Skipping automatic cloud provider detection at import time because " | |
| "an asyncio event loop is already running; defaulting to on-premise " | |
| "behavior until current_provider is explicitly set." | |
| ) |
Co-authored-by: octodog <[email protected]>
Avoid calling asyncio.run() when an event loop is already running to prevent runtime errors. Detection is skipped at module load if a loop is active, setting current_provider to None.
