I just had the same issue, but managed to solve it.I ran a test code like you, and with the same result.Turns out, the debugger resets the MCU (probably without halting it quick enough) so the flash-erase is initiated just before the debugger starts ...