סימן קכז סעיף יז
יָחִיד הַמִּתְעַנֶּה עַל צָרָתוֹ וְעָבְרָה, אוֹ שֶׁמִּתְעַנֶּה בִּשְׁבִיל חוֹלֶה וְנִתְרַפֵּא אוֹ מֵת, צָרִיךְ לְהַשְׁלִים כָּל הַתַּעֲנִיּוֹת שֶׁקִּבֵּל עָלָיו. וְכֵן אִם קִבֵּל עָלָיו תַּעֲנִיּוֹת אוֹ שְׁאָר מִצְוָה, עַד שֶׁיֵּעָשֶׂה בְּנוֹ בַּר מִצְוָה, וּמֵת הַבֵּן קֹדֶם, צָרִיךְ הוּא לְקַיֵּם אֶת נִדְרוֹ עַד הַשָּׁעָה שֶׁהָיָה רָאוּי לִהְיֹת בְּנוֹ בַּר מִצְוָה (עַיֵּן יורה דעה סִימָן רכ ברמ"א סָעִיף טו), אֲבָל אִם נוֹדַע לוֹ שֶׁקִּדֶם שֶׁקִּבֵּל עָלָיו לְהִתְעַנּוֹת, כְּבָר עָבְרָה הַסִּבָּה, דַּהָוָה קַבָּלָה בְּטָעוּת, אֵינוֹ צָרִיךְ לְהַשְׁלִים (תקסט).
If you fast because of personal trouble, and the trouble passed, or if you fast for a sick person, and that person recovered or died, you must complete all the fast days that you committed yourself to observe. If someone accepted upon himself to fast or to do some other mitzvah until his son becomes Bar Mitzvah,15This rule is not mentioned in Mishnah Berurah. (See Ramah, Yoreh Deiah 220:15) and the son died before that time, he must, nevertheless, fulfill his vow until the time his son would have become Bar Mitzvah. However, if you find out that before you committed yourself to fast, the cause for fasting had already passed16However, if the cause for fasting passed after you made the commitment, even though you had not actually started the fast, you must, nevertheless, complete the fast. (Mishnah Berurah 569:7) and your commitment, thus, was made in error, then you are not required to complete it.
תנא דבי אליהו כל השונה הלכות בכל יום מובטח לו שהוא בן העולם הבא שנאמר הליכות עולם לו אל תקרי הליכות אלא הלכות (נדה ע"ג.)