I came across a rare issue where a previously failed flash operation would cause a subsequent operation to fail as well. I tracked it down to stm32f4xx_hal_flash.c::HAL_Flash_Program()
When the function calls FLASH_WaitForLastOperation(), that function can return HAL_ERROR if a previous program operation failed. This causes the current operation to fail due to the subsequent check: if(status == HAL_OK)
I realize any previous call should clear the error bits after attempting to program, but this isn't guaranteed
I feel like the better check would be if(status != HAL_TIMEOUT), indicating the flash is still busy therefore the current operation cannot begin.