Another interpretation of "for the time of finding" (Psalm 32:6): for the time of the wringing-out of the soul. Said Rabbi Ishmael: A person must pray over the departure of the soul, for you have nothing harder than the wringing-out of the soul. How does it depart? Rabbi Yochanan said: Like a knot that passes out through the mouth of the gullet (that is, the rope at the time when they draw it through the hole of a ship to tie it fast by it) — so is the soul at the time when it departs from a person. For a person must not trust in himself until the day of his death; and thus our masters taught: "Do not trust in yourself until the day of your death" (Avot 2:4). It happened with a certain man, a sectarian, whose master would repeat and say, "Do not trust in yourself until the day of your death," and he would dispute the words of his master. The Holy One, blessed be He, said: This one has come to dispute against the Torah. What did He do to him? He tested him. He went out on the Day of Atonement into a certain garden and saw a certain young woman, adorned, lovely and praised, and she stood before him like a vessel, and he sought to attach himself to her. He spoke with her. She said to him: I am a married woman. He said to her: Accepted. She said to him: I am the wife of a priest. He said to her: Accepted. She said to him: I am a menstruant. He said to her: Accepted. And he did not budge until he had attached himself to her. He went and sat in his house in grief (that is, in pain), saying, What has passed over me? And his masters would come up and visit him, and they would find him like one ill, month after month, saying, What have I done? — yet he was not ill. What did the Holy One, blessed be He, do? He said: How long will he grieve? The Holy One, blessed be He, signaled to that demoness, and she went and stood before him. She said to him: How long will you grieve? I am that woman to whom you attached yourself; I am a spirit. Rather, it was because you were disputing the words of the sages, who said, "Do not trust in yourself until the day of your death." David too agreed to say that a person must pray over the departure of his soul; therefore he said, "for the time of finding."
The Sectarian and the Spirit Who Tested Him on Yom Kippur
Midrash Tanchuma Buber, Miketz 15:2
ד"א לעת מצא, לעת מיצוי הנפש. אמר (ר' יהושע) [ר' ישמעאל] צריך אדם שיהא מתפלל על יציאת הנפש, שאין לך דבר קשה מימיאת נפש. היאך היא יוצאה, ר' יוחנן אמר כפיטורי היוצאת בפי ושט (פי' החבל בשעה שמושכין אותו בנקב של ספינה לקשרה בו), כך הנפש בשעה שהיא יוצאה מן האדם, שאין אדם צריך להאמין בעצמו עד יום מותו, וכך שנו רבותינו, אל תאמין [בעצמך] עד יום מותך, מעשה באדם אחד מין שהיה רבו שונה ואומר אל תאמין בעצמך עד יום מותך, והיה חולק על דברי רבו, אמר הקב"ה זה בא לחלוק על התורה, מה עשה לו, ניסה אותו, ויצא יום הכפורים לתוך גינה אחת וראה ריבה אחת מקושטת נאה ומשובחת, ועמדה לו כנדו, ובקש להזקק לה, השיח עמה, אמרה לו אשת איש אני, אמר לה מקובל, אמרה לו כהנת אני, אמר לה מקובל, אמרה לו נדה אני, אמר לה מקובל, ולא זז עד שנזקק לה, הלך וישב לו לביתו אלופסין (פי' צעור) לומר מה עבר עליו, והיו רבותיו עולים ומבקרים אותו, ומוצאים אותו כחולה חודש אחר חודש, לומר מה עשיתי, והוא לא היה חולה, מה עשה הקב"ה, אמר עד מתי הוא מצטער, רמז הקב"ה לאותה שידה, והלכה ועמדה לפניו, אמרה לו עד מתי אתה מצטער, אני אותה האשה שנזקקתי עמי, רוח אני, אלא בשביל שהייתה חולק על דברי חכמים, שאמרו אל תאמין בעצמך עד יום מותך. אף דוד הסכים לומר צריך אדם להתפלל על יציאת נפשו, לכך אמר לעת מצוא.