2,051. How Far in Advance to Refrain
Hilchos Issurei Biah 4:11
Let’s say that a man was in the middle of intimacy with a woman who had been ritually pure but she now informs him that she has become ritually unclean. In such a case, he should not withdraw while still erect because doing so is as pleasurable as entering, with the result that withdrawing while erect can render one liable to kareis (spiritual excision), like one who enters. This same law applies to other prohibited relationships as well. In such a case, one should dig his toenails into the ground and remain motionless until he’s no longer erect, at which point he should withdraw.
Hilchos Issurei Biah 4:12
It is prohibited for a man to be intimate with his wife close to the time when she expects her period to begin out of concern that she might menstruate during the act, as per Leviticus 15:31, “You shall warn the children of Israel regarding their impurity.” If she normally begins her period by day, she is prohibited to be intimate starting from the beginning of the day; if she normally begins her period by night, she is prohibited to be intimate starting from the beginning of the night.