python / python/cpython

Profilers do not work in threads

Open
#124,656 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

This issue is related to, but not the same as #110770.
Running two profilers in parallel threads raises an unexpected error.
It's not clear at all that the profiler will not work in parallel.
Either this is a bug or there should be a prominent warning in the docs.

import time
from cProfile import Profile
from threading import Thread


def task_sleep() -> None:

    pr = Profile()
    pr.enable()
    time.sleep(1)
    pr.disable()


t1 = Thread(target=task_sleep)
t2 = Thread(target=task_sleep)

t1.start()
t2.start()

t1.join()
t2.join()
Exception in thread Thread-2 (task_sleep):
ValueError: Another profiling tool is already active
CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-124671

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the provided cProfile.Profile and threading example on Python 3.12, then inspect the profiler and thread interaction. Review linked PR gh-124671 and determine whether the accepted outcome is parallel profilers working without the ValueError or a prominent documentation warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.