Can't save coverage data if exited unexpectedly

Issue #501 invalid
Former user created an issue

Coverage stops in finally clause: https://bitbucket.org/ned/coveragepy/src/aa03c8e11f7054d0b334cfedd958286c952cf1ad/coverage/cmdline.py?at=default&fileviewer=file-view-default#cmdline.py-620:638

The coverage data can't be saved when Users use Ctrl+C to stop the program. As when using 'coverage run <program>', it is in a process. Sometimes the program can't exit by itself, it needs signal input. When using Ctrl+C to stop, the finally clause won't be executed.

I am wondering whether we can register a function in atexit to stop/save coverage.

Comments (5)

  1. Ned Batchelder repo owner

    Can you provide a reproducible failure? The code you linked to looks to me like it will work properly: the finally clause will run, code_ran will be True, and coverage.save() will be called. Additionally, in control.py, we already set an atexit handler to save the data.

  2. lixiaoy1

    For example, I use "coverage run test.py" to check code coverage of following file.

    test.py while i<4: time.sleep(1) print "again" i=i+2

    As test.py won't finish by iteself, I use Ctrl+C to kill the process. As a result, the finally clause won't be executed, and no coverage data is saved.

  3. Artem Dayneko

    Well, this is not reproducible (checked under 2.6, 2.7, 3.5). The following code in test.py is covered fine

    import time
    
    i = -100
    while i < 4:
        time.sleep(1)
        print "again"
        i += 2
    
    $ coverage run test.py
    again
    again
    again
    ^CTraceback (most recent call last):
      File "test.py", line 5, in <module>
        time.sleep(1)
    KeyboardInterrupt
    $ coverage report
    Name      Stmts   Miss  Cover
    -----------------------------
    test.py       6      0   100%
    
  4. space one

    Maybe he hit CTRL+C twice that another KeyboardInterrupt was raised in the finally block before the save() ran? (For large data this might work, but with that small example it can't be reproduced).

  5. Log in to comment