Thanks to user Christoph Rackwitz, the cause of the problem was identified as cv2.destroyAllWindows().
cv2.destroyAllWindows()
It seems to be present only on Mac systems and a bug report was filed, but apparently, there aren't currently any plans on fixing it.