Fix thread name CallsiteParameterAdder in async methods (issue #710)#805
Open
BlocksecPHD wants to merge 2 commits intohynek:mainfrom
Open
Fix thread name CallsiteParameterAdder in async methods (issue #710)#805BlocksecPHD wants to merge 2 commits intohynek:mainfrom
BlocksecPHD wants to merge 2 commits intohynek:mainfrom
Conversation
) When using async logging methods (.ainfo(), etc.), the CallsiteParameterAdder processor was collecting thread information from the executor thread pool instead of the caller's thread, causing thread_name to show 'asyncio_0' instead of the actual caller thread like 'MainThread'. This fix captures thread info (thread_id and thread_name) before passing control to the executor thread, storing it in a contextvar that is copied along with the calling stack frame. The processor then uses this captured thread info when available. Changes: - Add _ASYNC_CALLING_THREAD contextvar in contextvars.py - Update async methods in _native.py to capture thread info - Update async methods in stdlib.py (BoundLogger and AsyncBoundLogger) - Update CallsiteParameterAdder to use captured thread info from contextvar - Add tests for async thread info capture Fixes hynek#710
for more information, see https://pre-commit.ci
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.
Problem
When using async logging methods (
.ainfo(), etc.), theCallsiteParameterAdderprocessor was collecting thread information from the executor thread pool instead of the caller's thread. This causedthread_nameto show 'asyncio_0' instead of the actual caller thread like 'MainThread'.Solution
This fix captures thread info (thread_id and thread_name) before passing control to the executor thread, storing it in a contextvar (
_ASYNC_CALLING_THREAD) that is copied along with the calling stack frame. The processor then uses this captured thread info when available.Changes
_ASYNC_CALLING_THREADcontextvar to store captured thread infoBoundLoggerandAsyncBoundLoggerto capture thread info_get_callsite_threadand_get_callsite_thread_nameto use captured info from contextvarTesting
All tests pass:
Example Output
Before fix:
After fix:
Fixes #710