Skip to content

sys.monitoring use_tool_id exception #2187

Closed

Description

@gnufede

Describe the bug
If coverage.py with the sys.monitoring API is used at the same time as another tool is using it to collect coverage, an unhandled exception will be raised:

ValueError: tool 1 is already in use

To Reproduce
Using Python 3.12+

# repro.py
import sys
import coverage


tool_id = sys.monitoring.COVERAGE_ID
sys.monitoring.use_tool_id(tool_id, "another coverage tool")

cov = coverage.Coverage(config_file=False)
cov.start()

$ COVERAGE_CORE=sysmon python repro.py

Expected behavior
The Exception is handled instead of exiting the process.

Additional context
Add any other context about the problem here.

Activity

  1. added
    bug
    on Jun 5, 2026
  2. nedbat commented on Jun 5, 2026

    @nedbat
    Member

    If coverage.py with the sys.monitoring API is used at the same time as another tool is using it to collect coverage

    What is the other tool?

  3. gnufede commented on Jun 5, 2026

    @gnufede
    Author

    What is the other tool?

    dd-trace-py used like pytest --ddtrace in my case

  4. nedbat commented on Jun 5, 2026

    @nedbat
    Member

    What would you like coverage to do? Raise a different exception? dd-trace-py does this:

        coverage_tool = sys.monitoring.get_tool(sys.monitoring.COVERAGE_ID)
        if coverage_tool is not None and coverage_tool != "datadog":
            log.debug("Coverage tool '%s' already registered, not gathering coverage", coverage_tool)
            return code, CoverageLines()
  5. nedbat commented on Jun 5, 2026

    @nedbat
    Member

    In retrospect, the sys.monitoring tool-id scheme seems too simplistic. Why pre-allocate an ID to coverage if more than one tool might want to use it?

  6. gnufede commented on Jun 5, 2026

    @gnufede
    Author

    Yeah, the issue is that dd-trace-py allocates the tool earlier, so the exception is raised by coverage in a later pytest hook. I've tried splitting in two phases dd-trace-py code, allowing coverage to start in the middle, but it breaks the functionality.

    Maybe the solution is for dd-trace-py to use a different tool id altogether, but this is for an internal coverage-based tool, so registering as a coverage tool was the natural move.

    My hunch was that coverage could capture the ValueError, log a warning or similar, and let the pytest process continue, but not sure how this is expected to be handled to be honest.

  7. nedbat commented on Jun 5, 2026

    @nedbat
    Member

    My hunch was that coverage could capture the ValueError, log a warning or similar, and let the pytest process continue, but not sure how this is expected to be handled to be honest.

    Maybe we need to take a step back: why are both tools running at once? Do you want them to both capture data?

  8. gnufede commented on Jun 8, 2026

    @gnufede
    Author

    Sorry Ned, this was definitely missing some context.

    First of all, allow me to thank you for the great job you're doing with coverage.py.

    The issue here is when pytest is run with both plugins pytest-cov and dd-trace-py (this latter one under certain config), that collect coverage for different purposes.

    The use case is as follows: pytest-cov would keep track of coverage at session level, and report it as usual. Simultaneously, dd-trace-py may be collecting coverage on a per-test basis, for a feature of selective testing called Test Impact Analysis, that will allow test skipping in subsequent test executions if the code changes and the specific test to be executed are unrelated.

    In short, I think dd-trace-py could use a different tool id to avoid collisions, but I also think that this exception should be handled, allowing test execution to take place. Probably the place to handle this exception is pytest-cov instead of coverage.py, although raising a more specific exception may be useful for pytest-cov.

  9. nedbat commented on Jun 8, 2026

    @nedbat
    Member

    In commit 4b0fc85 I've changed the logic to search for an unused tool id, fixing this.

  10. nedbat commented on Jun 20, 2026

    @nedbat
    Member

    This is now released as part of coverage 7.14.2.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions