fix: allow @metric_scope sync wrappers to work inside asyncio event loops#141
Open
jaheyy wants to merge 1 commit intoawslabs:masterfrom
Open
fix: allow @metric_scope sync wrappers to work inside asyncio event loops#141jaheyy wants to merge 1 commit intoawslabs:masterfrom
jaheyy wants to merge 1 commit intoawslabs:masterfrom
Conversation
d99be00 to
0cf359e
Compare
…oops Add resolve_environment_sync() that uses cached environment when available and falls back to a worker thread for first-time async resolution inside a running event loop. Add flush_sync() to MetricsLogger that uses the sync resolver. Sync wrappers in @metric_scope now call flush_sync() instead of asyncio.run(flush()), fixing RuntimeError when used inside a running loop. Added tests to ensure @metric_scope works when sync functions are called from within an async context.
0cf359e to
128b620
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #131
Summary
Add synchronous flush and environment resolution paths that work regardless of whether an event loop is running:
•
resolve_environment_sync()inenvironment_detector.py— checks for a cached environment first, then detects whether an event loop is running. If no loop is running, usesasyncio.run(). If a loop is running, offloads the async resolution to aThreadPoolExecutorto avoid theRuntimeError.•
flush_sync()inMetricsLogger— callsresolve_environment_sync()and flushes the context synchronously. Shares the same__flush_with_environment()helper as the asyncflush().• Sync wrappers in
metric_scope/__init__.pynow calllogger.flush_sync()instead ofasyncio.run(logger.flush()).The async
flush()path is unchanged — this is purely additive for the sync codepath.Testing
• 4 new tests covering
resolve_environment_sync,flush_sync, and both sync function/generator wrappers inside a running event loopDependencies