VSCode Python Debugger steps on lines not being executed--and over lines being executed

Viewed 1040

I'm working on a Python project, and I've been using the VS Code Python debugger frequently, using breakpoints or just using the step over/into tool to understand how my code executes.

After making some changes to a single Python file, the debugger started behaving strangely. While the script itself is executing as expected (based on logs and expected side effects), when I step through the script with the debugger it skips over lines that clearly are being executed, and stops on lines that clearly are not being executed.

For example, after setting an early breakpoint and using only "Step Over" to navigate through the execution of my script, the debugger highlighted the following lines, individually in sequence:

  1. An if statement (normal [expected to resolve as true based on variable values])
  2. Each line of the if block (normal)
  3. The first line of the corresponding else block (not normal)--without stopping on the else statement itself (also not normal)
  4. The second line of a try block, without stopping on the try statement itself, or on the first line of the try block (not normal)--and without stopping on any of the other lines in the above else block

... (many more lines)

  1. The second line of an if block, for which the if statement resolved to false, without stopping on the if statement itself or on the first line of the if statement (not normal)
  2. The second line of a finally block, without stopping at the else statement or else block corresponding to the above statement(not normal)--or on the finally line itself, or on the first line of the finally block (not normal)

Again, I can clearly tell this is not how the code is executing. Referring to steps 5 and 6 above: the code generates an email as coded in the else case, and does not generate an email as coded in the if case.

If I revert the file to its previous state, the debugger behaves normally. Using git diff, I can tell there are no differences in blocking, parentheses, whitespace, etc. between the previous version and this one.

Am I missing something? I don't believe I ever had this issue before.

  1. I don't understand how changing the code would change the behavior of the debugger
  2. This issue renders the debugger essentially useless for my intention of understanding the behavior of my code.
2 Answers

I'll try to explain, let's suppose you've the code:

1 if True:
2     if False:
3         a = 1
4 else:
5     b = 2
6 try:
7     c = 3

Then you run Python with debugger, it stops on 1st line. Next you remove 2nd line in IDE and got actual code:

1 if True:
2     a = 1
3 else:
4     b = 2
5 try:
6     c = 3

Many frameworks uses watchdogs in development mode to autoreload project when source files changed (also some debuggers could do that on-the-fly for specific changed module). But if your edit doesn't trigger reloading of Python you've got situation where script executes lines in order:

  • 1 (if True)
  • 2 (if False)
  • 4 (else)
  • 6 (try)

and IDE will stops on these lines but with wrong statements:

  • 1 (if True)
  • 2 (a = 1)
  • 4 (b = 2)
  • 6 (c = 3)

I was seeing the exact same problem (lines appearing to be skipped during debug, etc.), probably caused by ptvsd not working with the actual up-to-date source file. I've managed to work around this by adding the following to my workspace settings, which disables an experiment:

"python.experiments.optOutFrom": [
    "PtvsdWheels37 - experiment"
],

Without the above workaround, the problem occurs sporadically for me -- I haven't been able to pin down exactly when it occurs. I've created the issue https://github.com/microsoft/vscode-python/issues/11716 on this.

Related