Disabled coverage CTracer may rise from the dead

Issue #436 resolved
David MacIver
created an issue

The following code causes an issue for me where I have changed away from the coverage tracer to a tracer of my own devising, but the coverage CTracer mysteriously reappears.

import sys


def tracer(frame, event, arg):
    return tracer


def begin():
    sys.settrace(tracer)


def collect():
    t = sys.gettrace()
    assert t is tracer, t


def test_unsets_trace():
    begin()
    collect()


if __name__ == '__main__':
    test_unsets_trace()

Running the above code under 'python -m coverage run test_coverage_unset_trace.py' (or whatever you call the file) under Python 3.4.3 or 3.5.0 with coverage 4.0.1 produces the following error for me:

Traceback (most recent call last):
  File "test_coverage_regression.py", line 23, in <module>
    test_unsets_trace()
  File "test_coverage_regression.py", line 19, in test_unsets_trace
    collect()
  File "test_coverage_regression.py", line 14, in collect
    assert t is tracer, t
AssertionError: <coverage.CTracer object at 0x7fbdfa585a40>

Note: I do not see this issue on Coverage 4.0.0, or on Python 2.7 running 4.0.1. This seems to be specifically 4.0.1 and Python 3 together that cause the problem.

I originally believed this to be related to the fix for https://bitbucket.org/ned/coveragepy/issues/397/stopping-and-resuming-coverage-with, but I can't see why the tracer object would be running via settrace at this point rather than C level tracing, so this might be a red herring.

If I run this with --timid I get the "trace function changed" warning but do not see this problem.

Comments (5)

  1. Ned Batchelder repo owner

    @David MacIver OK, it's easy for these experiments to skitter around. I see the same results on 2.7 and 3.5:

    $ python --version
    Python 2.7.10
    $ pip install coverage==4.0
    Collecting coverage==4.0
      Using cached coverage-4.0-cp27-none-macosx_10_10_x86_64.whl
    Installing collected packages: coverage
    Successfully installed coverage-4.0
    $ coverage run bug436.py
    $ pip install coverage==4.0.1
    Collecting coverage==4.0.1
      Using cached coverage-4.0.1-cp27-none-macosx_10_10_x86_64.whl
    Installing collected packages: coverage
      Found existing installation: coverage 4.0
        Uninstalling coverage-4.0:
          Successfully uninstalled coverage-4.0
    Successfully installed coverage-4.0.1
    $ coverage run bug436.py
    Traceback (most recent call last):
      File "bug436.py", line 23, in <module>
        test_unsets_trace()
      File "bug436.py", line 19, in test_unsets_trace
        collect()
      File "bug436.py", line 14, in collect
        assert t is tracer, t
    AssertionError: <coverage.CTracer object at 0x108a8d3c8>
    
    $ python --version
    Python 3.5.0
    $ pip install coverage==4.0
    Collecting coverage==4.0
      Using cached coverage-4.0-cp35-cp35m-macosx_10_10_x86_64.whl
    Installing collected packages: coverage
    Successfully installed coverage-4.0
    $ coverage run bug436.py
    $ pip install coverage==4.0.1
    Collecting coverage==4.0.1
      Using cached coverage-4.0.1-cp35-cp35m-macosx_10_10_x86_64.whl
    Installing collected packages: coverage
      Found existing installation: coverage 4.0
        Uninstalling coverage-4.0:
          Successfully uninstalled coverage-4.0
    Successfully installed coverage-4.0.1
    $ coverage run bug436.py
    Traceback (most recent call last):
      File "bug436.py", line 23, in <module>
        test_unsets_trace()
      File "bug436.py", line 19, in test_unsets_trace
        collect()
      File "bug436.py", line 14, in collect
        assert t is tracer, t
    AssertionError: <coverage.CTracer object at 0x101d700e8>
    
  2. David MacIver reporter

    The "Trace function changed" message is something I've only ever seen when running with the pure tracer. Is your 3.5 build not using CTracer for some reason?

    Interesting that you see it on 2.7 though! I'm pretty sure I didn't, but I might have screwed up.

  3. David MacIver reporter

    It does make more sense that you'd see the same behaviour on 2.7 and 3.5 TBH. I'm going to assume I did something wrong that was causing me to not observe this behaviour on 2.7 - probably I had it running with the pure tracer, or the wind was in the wrong direction or something. :-)

  4. Log in to comment