To have good luck in the midst of bad luck.

A blessing in disguise.
A good thing you don’t recognize at first.