Case in screenshot 1 should not occur. It should always be like screenshot 2. This is yet another bug introduced by the optimizations done in ICE-4106. A value which should be calculated dynamically on the spot was "optimized" to be calculated only at the start of the function.
In screenshot 2, the menu is not wrongly positioned on top of the menu bar, it is to avoid the menu positioning beyond the top of the viewport. This is a requirement of ICE-3658. Do we want to revert that?
If the space above is larger than the space below, then better position menu above even if can't fit completely. Ohterwise if the menu bar is close to the bottom of the viewport user would complain again why we don't put the menu above.
Another tweak to the menu positioning logic. I'd suggest that you don't position the menu above the menubar unless it can fit completely in the available space.
Also, the top of the menu should always be under (or over) the menu-bar, not on top of it.