Skip to content

gh-135953: Properly obtain main thread identifier in Gecko Collector#146045

Open
flowln wants to merge 1 commit intopython:mainfrom
flowln:fix_tachyon_main_thread
Open

gh-135953: Properly obtain main thread identifier in Gecko Collector#146045
flowln wants to merge 1 commit intopython:mainfrom
flowln:fix_tachyon_main_thread

Conversation

@flowln
Copy link

@flowln flowln commented Mar 17, 2026

Since running a profiler via CLI (python -m profiling.sampling run) spawns a new subprocess where the actual user-specified code will run, a call to threading.main_thread() in the collector's process will not return the profiled process's main thread.

To combat this, we rely on the fact that thread objects are inserted in such a way that the first object in the list represents the oldest ThreadState object, which corresponds to a ThreadState associated with the main thread.

This allows the main thread to be highlighted on Firefox Profiler:
image

Since running a profiler via CLI (python -m profiling.sampling run)
spawns a new subprocess where the actual user-specified code will run,
a call to threading.main_thread() in the collector's process will not
return the profiled process's main thread.

To combat this, we rely on the fact that thread objects are inserted
in such a way that the first object in the list represents the oldest
ThreadState object [1], which corresponds to a ThreadState associated
with the main thread.

[1] - https://github.com/python/cpython/blob/1b118353bb0a9d816de6ef673f3b11775de5bec5/Include/internal/pycore_interp_structs.h#L831

Signed-off-by: Sofia Donato Ferreira <[email protected]>
@flowln flowln requested a review from pablogsal as a code owner March 17, 2026 02:12
@bedevere-app
Copy link

bedevere-app bot commented Mar 17, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link

python-cla-bot bot commented Mar 17, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant