Good job, really nice work figuring it out with the steps to reproduce.
I'll try and see if I can get the same to happen, next time I'm at my gaming PC.
This could be a bug in Optifine - especially with it happening differently if Minecraft is restarted, we are not doing that. We will look to...