Skip to content

Conversation

@Yaminyam
Copy link
Member

@Yaminyam Yaminyam commented Jan 9, 2026

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.
CleanShot 2026-01-09 at 11 24 32@2x

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.
Copilot AI review requested due to automatic review settings January 9, 2026 02:24
@github-actions github-actions bot added size:XS ~10 LoC comp:common Related to Common component labels Jan 9, 2026
@Yaminyam Yaminyam requested a review from HyeockJinKim January 9, 2026 02:25
Yaminyam and others added 2 commits January 9, 2026 11:29
Copy link
Contributor

Copilot AI left a 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_provider to None when 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.

Comment on lines +148 to +151
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
Copy link

Copilot AI Jan 9, 2026

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".

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Jan 9, 2026

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.

Suggested change
# 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."
)

Copilot uses AI. Check for mistakes.
@HyeockJinKim HyeockJinKim added this pull request to the merge queue Jan 14, 2026
Merged via the queue into main with commit 84b2179 Jan 14, 2026
28 checks passed
@HyeockJinKim HyeockJinKim deleted the fix/detect-cloud-event-loop branch January 14, 2026 15:52
jopemachine pushed a commit that referenced this pull request Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:common Related to Common component size:XS ~10 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants