The bug
When trading with a villager, they don't stop in place straight in front of you when the trading interface is up. So when the villager gets too far away from you, the trading interface automatically closes.
In the first picture, you can see the villager I just right-clicked on has moved all the way to the lamppost to the left and he kept going. And in the second picture, you can see the same one in the bottom left-corner.
I cannot reproduce this. The villager will stop all tasks that he planned to do (like going to bed or working) just to trade with me.