Can't save coverage data if exited unexpectedly

Issue #501 invalid
Former user created an issue

Coverage stops in finally clause:

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 will be called. Additionally, in, we already set an atexit handler to save the data.

  2. lixiaoy1

    For example, I use "coverage run" to check code coverage of following file. while i<4: time.sleep(1) print "again" i=i+2

    As 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. Артем Дайнеко

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

    import time
    i = -100
    while i < 4:
        print "again"
        i += 2
    $ coverage run
    ^CTraceback (most recent call last):
      File "", line 5, in <module>
    $ coverage report
    Name      Stmts   Miss  Cover
    -----------------------------       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