If the returned array is empty, your for loop will have a wrong range:
for i in range(path.size() - 1, 0, -1):
for i in range(-1, 0, -1):
Also I see you are removing elements from the array while you are iterating on it. That can work, but make sure you know what you are doing.
if path.size() > 0: # Error triggered here
The error you quoted doesn't happen here, there is no way. Maybe the debugger reports the wrong line?