Flash erase not working

I am using STM32L476RG for my application and I want to erase flash in order to write something into it. I am also using an interrupt for my application. Erasing the flash and writing into it works fine before the interrupt is called. But after the interrupt, erasing the flash results in error - FLASH_FLAG_SIZERR. I am unable to figure out the reason for this. Although in my flash_erase function , replacing __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR) with __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS) works fine, I want to know the reason for the error.