The commit that introduced this code is 12788ae49e1933f463bc. So I amn copying Heikki.
AFAICR the commit was mostly a heavy restructuring of previous unmaintainable spaghetti code. I'm not sure the problem was not there before under one form or another.
I agree that it should error out & stop the client in this case at least.
Here is a probable "fix", which does was the comment said should be done.
Looks good to me.
I could not trigger an infinite loop with various kill -9 and other quick stops. Could you try it on your side?
Ok, I will try. But TBH I did not try to reproduce that either and I am not sure if I can. I discovered the problem when my laptop's battery started draining out much more quickly. Having seen the problem, it seems very obvious though.