Leading to the following patch:
Thanks for the fix! It works! Woohoo! :D
It was a nasty bug, very visually disorienting, very difficult to reproduce in the beginning, but ironically got fixed by 1 line.
Thanks for spending time to debug this and come up with that crucial fix. Did any of the within-emacs debug tactics help you figure this out? Or was it putting breakpoints and catching them in gdb? Or did you go the final hard way, and review the code manually?
Also thanks to everyone who helped figure out a recipe, which is equally important :).