fix(profiling): asyncio profiling outside running loop#17165
Draft
KowalskiThomas wants to merge 2 commits intomainfrom
Draft
fix(profiling): asyncio profiling outside running loop#17165KowalskiThomas wants to merge 2 commits intomainfrom
KowalskiThomas wants to merge 2 commits intomainfrom
Conversation
Co-authored-by: KowalskiThomas <14239160+KowalskiThomas@users.noreply.github.com>
Co-authored-by: KowalskiThomas <14239160+KowalskiThomas@users.noreply.github.com>
|
I can only run on private repositories. |
Base automatically changed from
dd/kowalski/gevent-greenlet-untracking-keyerror
to
main
March 27, 2026 15:28
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.
Description
This fix addresses a critical bug in the asyncio profiling module where task linking and loop tracking would fail when profiling was initialized before the event loop started running. The issue occurred in two scenarios:
Loop tracking:
asyncio.get_running_loop()raisesRuntimeErrorwhen called outside a running loop context, preventing the profiler from tracking loops that were set viaasyncio.set_event_loop()but not yet running.Task linking: Direct calls to
asyncio.current_task()would fail withRuntimeErrorin non-running contexts, breaking parent-child task relationships and loop.create_task() instrumentation.The fix introduces safe helper functions to gracefully handle these cases:
_current_task_or_none(): Safely returns the current task or None instead of raising RuntimeError_get_current_or_thread_loop(): Checks both running loop and thread-local policy state to recover already-set loopsBaseEventLoop.create_task()to track direct loop task creationTesting
Two new test files ensure the fix works correctly:
tests/profiling/test__asyncio.py: Unit tests for loop tracking in non-running contextstests/profiling/collector/test_asyncio_loop_create_task.py: Integration tests for loop.create_task() instrumentationPreviously failing tests in
test_asyncio_import_order.pyare now enabled and passing:test_asyncio_start_profiler_from_process_after_creating_looptest_asyncio_import_profiler_from_process_after_starting_loopRisks
Minimal risk. Changes are defensive and all operations are guarded with None checks. The fix only affects error paths that previously would have crashed.
Additional Notes
The implementation accesses internal asyncio event loop policy state (
_localand_loopattributes) as documented in the code comments. This is necessary becauseasyncio.get_event_loop()would create a new loop rather than return the already-set one.PR by Bits - View session in Datadog
Comment @DataDog to request changes