Hi,
From hardware perspective, when you erase/program flash, the current will increase, so it is possible that the power supply voltage will decrease, I suggest you use a LDO or DC/DC converter which can provide enough current.
From software perspective, I suspect that you have the code to erase/program flash in your application code, as you know that flash operation instruction is saved in ROM and run in ROM, when you erase/program flash, you must disable all interrupt, disable watchdog lest the flash code is executed.
I copy the section from UM10912.pdf
"
section 5.6 In-Application Programming
The flash memory is not accessible during a write or erase operation. IAP commands, which results in a flash write/erase operation, use 32 bytes of space in the top portion of the on-chip RAM for execution. The user program should not be use this space if IAP flash programming is permitted in the application.
"
Hope it can help you
BR
XiangJun Rong