Cutting Off the Prayer
Question :
If I forgot, and prayed in a garment on which there was some filth, then I remembered it during the prayer, is it permissible for me to cut off the prayer and change it? And what are the situations in which it is permissible to cut off the prayer?
Answer:
Whoever prayed while he is carrying some kind of filth of which he is aware, his prayer will be invalid, and if he does not know about it until after he has completed his prayer,
it will be accredited to him and he will not be required to make up for it. If he becomes aware of it during his prayer and he is able to remove it quickly, he may do so, and complete his prayer, because it has been authentically reported that the Prophet removed his shoes once while he was praying when Jibril informed him that there was something harmful on them and it did not invalidate the beginning of his prayer. Likewise, if it was in his headdress and he removed it quickly, he should continue in accordance with what he has already prayed, however, if it required actions such as the removal of the shirt, Sirwal, etc., then after removing it, he should resume his prayer. Likewise, he must cut off the prayer if he passes wind during the prayer, or it is invalidated by laughing etc.
Source:
Ibn Jibreen
Fatawa Islamiyah, Vol. 2 Pages 207-208