Leonardo USB won't stay connected

SoylentGraham:
Okay, with a bit of luck and good timing, I managed to get blink to compile & upload before it disconnects and the problem has gone away!

Clearly my code did mess up the USB connection in someway...

Not likely. If it happens again, try putting a USB Hub in-between the PC and the Leonardo. It smooths these glitches for me.