Rob Watts
Rob Watts

Reputation: 7146

How can the line numbers in my stack traces be wrong?

I have a python (version 2.7.6) program that had been running for a day or two as of last night when it reported some errors. However, the stack traces were blatantly wrong. Pretend my code is like this:

def do_A():
    do_some_stuff()
    do_B()

def do_B():
    do_some_IO_that_could_fail()

def do_C():
    if len('abc'):
        do_D()

def do_D():
    do_other_stuff()

if __name__ == '__main__':
    do_A()

The task that can fail did fail, but my stack trace was like this:

Traceback (most recent call last):
  File "myfile.py", line 16, in <module>
    do_A()
  File "myfile.py", line 9, in do_A
    if len('abc'):
  File "myfile.py", line 13, in do_B
    do_other_stuff()
  CustomFailureException: "The thing that can fail? It failed."

In my stack trace, the "in ..." parts are reasonable in that they describe a path that can actually get to the part of my code that has the CustomFailureException. The line numbers and the code it shows are consistent (i.e it says line 9 and has the code that's on line 9), but it's the wrong line.

Does anyone have any clue how this could happen to my stack trace?

Note: This particular task runs every night, and has been doing so for a while. It normally finishes successfully, but it has failed before and given a normal stack trace. Last night, it consistently failed and consistently gave the same incorrect stack trace.

Upvotes: 4

Views: 2504

Answers (1)

strubbly
strubbly

Reputation: 3477

This happens when you change the source file after the code is running. The compiled source has line number references to the source code, but the stack traceback fills in the actual source text by reloading the source file. I think there is some kind of caching as well but that's not relevant here.

So the problem here is just that the source file no longer matches the (compiled) program that is actually running, likely because it was changed after the program started or perhaps they have become out of sync by some more complicated trickery ...

Upvotes: 6

Related Questions