And one may not sleep in the beit knesset, even a brief nap. However, in a beit midrash it is permitted for one who studies there. For the needs of the beit knesset, it is permitted to eat and sleep within them, provided that one does not bring a bed there (Magen Avraham, subsection 4). For this reason, they sleep on the night of Yom Kippur in the beit knesset in order to guard the candles, so that no mishap occurs. Furthermore, those who sleep there recite much Tehillim, songs, and praises. Similarly, for the purpose of a mitzvah it is permitted, such as in ancient times when they would enter the beit knesset for the intercalation of the year and would not go to their homes at all—they were able to eat and sleep there. However, they would only subsist on bread and legumes. Similarly, when they make a siyum on a masekhta, and certainly on more, they may eat a little in the beit midrash, as this is a seudat mitzvah. To establish a large feast there appears to be inappropriate, and one should not do so except in a case of great necessity. This applies specifically to a seudat mitzvah of Torah study, but for another mitzvah, such as a seudat brit milah, or pidyon haben, or a wedding—it is forbidden, as these have no connection to the beit midrash.
ואין ישנים בבית הכנסת אפילו שינת עראי. אבל בית המדרש מותר למי שלומד שם. ולצורך בית הכנסת מותר לאכול ולישן בתוכן, רק לא להכניס מיטה לשם (מגן אברהם סעיף קטן ד'). ומטעם זה ישנים בליל יום הכיפורים בבית הכנסת כדי לשמור הנרות, שלא יארע סיבה. וגם הישנים שם אומרים הרבה תהלים ושירות ותשבחות. וכן לצורך מצוה מותר, כמו בזמן הקדמון שהיו נכנסים לעיבור השנה לבית הכנסת, ולא היו הולכים לבתיהם כלל – יכולים לאכול ולישן שם. אך הם היו עולים רק בפת וקטניות. וכן כשעושים סיום על מסכתא, וכל שכן על יותר – יכולים לאכול מעט בבית המדרש, דזהו סעודת מצוה. ולקבוע שם סעודה גדולה נראה שאינו כדאי, ואין לעשות כן רק בדוחק גדול. וזהו דווקא בסעודת מצוה של תלמוד תורה, ולא של מצוה אחרת כסעודת ברית מילה או פדיון הבן או חתונה – אסור, דאין לזה שייכות לבית המדרש.