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

筅森魡賤 提交于 2019-12-10 18:08:42

问题


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.


回答1:


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 ...



来源:https://stackoverflow.com/questions/33175257/how-can-the-line-numbers-in-my-stack-traces-be-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!