HETERIM TO EAT OR SLEEP IN A BEIS HA'KENESES OR BEIS MEDRASH
Rav Ashi saw cracks in the Beis ha'Keneses of Masa Nachsiya. He destroyed it, and (to make people zealous to build it quickly) moved his bed there until (even) the gutter pipe was installed.
Megilah 28b (Beraisa): We do not eat or drink in a Beis ha'Keneses. We do not enter a Beis ha'Keneses to avoid heat or rain.
Rav Asi: Batei Kenesiyos in Bavel are made on Tanai (a stipulation). Nevertheless, we do not act in them frivolously, i.e. to make calculations;
Rava: Chachamim are permitted.
Rava was teaching Ravina and Rav Acha bar Masnah. A torrential rain came; they entered a Beis ha'Keneses. They did not enter to avoid the rain, rather, for the sake of learning, which requires clarity.
Berachos 25a (Rav Sheshes): One may learn Torah if there is a bad smell without a source. Look at the mattresses of the Beis Medrash. Some Talmidim learn while others sleep (and pass wind).
Sanhedrin 71a (R. Zeira): "U'Kra'im Talbish Numah" - if one sleeps in a Beis Medrash, his Torah becomes fragmented (he remembers only parts).
Pesachim 101a: Shmuel requires Kidush to be in the place where one eats. He holds that we make Kidush in the Beis ha'Keneses for visitors who eat, drink and sleep in the Beis ha'Keneses. They are Yotzei through it.
The Rif and Rosh (Megilah 9a and 4:7) bring the Gemara in Megilah. Their text of the Beraisa discusses Batei Kenesiyos and Batei Medrashos.
Rosh: Even though Batei Kenesiyos in Bavel are made on Tanai, we do not permit frivolity. Rather, the condition helps for when they are desolate.
Rif and Rosh (Berachos 16b and 3:47): ...In the Beis Medrash some Talmidim learn while others sleep...
Rif and Rosh (Pesachim 19b and 10:5): Kidush in the Beis ha'Keneses is Motzi visitors who eat, drink and sleep there.
Ran DH Le'afukei): Even though we do not eat or drink in a Beis ha'Keneses, perhaps for visitors it is considered a Mitzvah of the Rabim, for they are fed by the Tzedakah box of the city. Alternatively, they eat in rooms next to the Beis ha'Keneses.
Rambam (Hilchos Tefilah 11:6): Chachamim may eat or drink in a Beis ha'Keneses or Beis Medrash b'Dochak (it is difficult to do so elsewhere).
Kesef Mishneh (7): The Tanai allows benefit from them (even while they are in use), but not lightheadedness, i.e. calculations.
Rambam (Hilchos Shabbos 29:8): We make Kidush in the Beis ha'Keneses to be Motzi visitors who eat there.
Magid Mishnah: The visitors hear Kidush and eat in houses next to the Beis ha'Keneses, for one may not eat in a Beis ha'Keneses.
Hagahos Maimoniyos (5): This is unlike Rashi (28b DH Tanai), who says that the Tanai allows benefit from them (even while they are in use). One may eat and sleep in a Beis ha'Keneses for its sake (e.g. to guard it), or even for the sake of other Mitzvos.
Ran (DH Batei): The Ramban explains that the Tanai allows the city to feed or lodge poor people there, if necessary. Also, Chachamim may benefit from them, but only if necessary. They may not enter merely to avoid rain. A Beis Medrash is 'Bei (the house of) Rabanan', for they learn there all day. A Beis ha'Keneses is not Bei Rabanan.
Kesef Mishneh: R. Yehoshua ben Levi teaches that a Beis Medrash is 'Bei Rabanan'. All the more so a Beis ha'Keneses, which (he himself holds) is less Kodesh is Bei Rabanan. Nevertheless, we needed to answer why they entered during the rain. This teaches that even Chachamim may benefit from a Beis Medrash only when there is no alternative.
Rosh (Bava Basra 1:4): Rav Ashi did not really move his bed into the Beis ha'Keneses. We may not eat or sleep in a Beis ha'Keneses! Rather, he moved in nearby, where guests eat and sleep, like it says in Pesachim. Rashi says that Batei Kenesiyos in Bavel are made on Tanai, and permits doing all needs there even when they are settled. He can say that Rav Ashi moved his bed into the Beis ha'Keneses itself. This is difficult, for we needed to explain that Rav Papa and Rav Ada did not enter merely to escape the rain! Rather, the Tanai helps for after they become desolate.
Shulchan Aruch (OC 151:1): We do not eat or drink in a Beis ha'Keneses or Beis Medrash. Chachamim may eat or drink there b'Dochak
Beis Yosef (DH v'Rabeinu): The Rambam permits Chachamim to eat and drink in a Beis ha'Keneses b'Dochak. He learns from Rava and his Talmidim. The Gemara needed to justify why they entered during the rain, for escaping the rain is not considered Dechak. Many people walk in the market in the rain. Or Zaru'a (in Hagahos Ashri) allows without Dechak. He says that Rava and his Talmidim were stringent on themselves, and that anyone may eat and drink in a Beis ha'Keneses or Beis Medrash. The Tur permits Chachamim. It appears that he holds like Tosfos and the Rosh that this is only b'Dochak, even for Batei Kenesiyos in Bavel, which are on Tanai. He did not specify so because he holds that this is obvious
Magen Avraham (2): Surely the Heter for Chachamim to eat and drink in a Beis ha'Keneses is to avoid Bitul Torah of returning home whenever they needed to eat. If they do not learn there, it is forbidden. Also Chachamim are commanded to fear the Mikdash! The Or Zaru'a agrees. He permits only in Bavel, because their Batei Kenesiyos are on Tanai.
Mishnah Berurah (5): Similarly, the city may feed or lodge poor people there if necessary. This applies only in Chutz la'Aretz, and some forbid even in Chutz la'Aretz.
Mishnah Berurah (6): Chachamim may eat or drink or sleep there.
Rema: Some permit a Beis Medrash to Chachamim even not b'Dochak.
Bi'ur Halachah (DH v'Yesh): The Ran allows Chachamim even to enter escape bad weather, since it is their house (but frivolity is forbidden). This does not apply to a Beis ha'Keneses, even b'Dochak. The Rema did not specify. It seems that he holds like the Rambam, who allows only eating and drinking. Presumably, other needs are included.
Mishnah Berurah (9): One who learns all day in a Beis Medrash may rely on the Rema.
Kaf ha'Chayim (13): Acharonim are lenient even if it is not b'Dochak. A Chacham may rely on them. It is as if the Beis Medrash was built on this Tanai. Perhaps 'b'Dochak' means to avoid Bitul Torah. Even a Chacham should not eat in a Beis ha'Keneses unless there is a great need, e.g. he cannot learn otherwise. The same applies to smoking.
Kaf ha'Chayim (9): Even water is forbidden in a Beis ha'Keneses or Beis Medrash, except for a Chacham who learns there. Today the custom is to be lenient about water for one who is learning, even if he is not a Chacham, to avoid Bitul Torah.
Kaf ha'Chayim (12): Some permit visitors to eat even in a Beis ha'Keneses of Eretz Yisrael. The Isur to eat there applies only to residents of the city.
Shulchan Aruch (3): One may not sleep in a Beis ha'Keneses, even Arai (casually). It is permitted in a Beis Medrash.
Mishnah Berurah (15,16): A Chacham may sleep Arai in a Beis ha'Keneses, and even Keva (in a fixed way) in a Beis Medrash. The Shulchan Aruch discusses others. They may sleep Arai in a Beis Medrash. R. Akiva Eiger was hesitant about this.
Kaf ha'Chayim (30): The Heter for Chachamim applies equally to a Beis ha'Keneses, perhaps even more so, for it is less Kodesh.
Kaf ha'Chayim (29): It is even worse to sleep during the Drashah. "One who averts hearing Torah, his Tefilah is detested."
Shulchan Aruch (4): For the needs of a Beis ha'Keneses or another Mitzvah, one may eat and sleep there.
Source (Beis Yosef): The Yerushalmi says that we must check a Beis ha'Keneses for Chametz because people eat there for Ibur Shanah (when they add a month to the year).
Mishnah Berurah (17): The Magen Avraham forbids bringing a bed in, just like it is not proper to bring beds into the Mikdash (Tamid 26b). Others permit.
Mishnah Berurah (18): Semak permits a Seudas Mitzvah only like that of Ibur Shanah, which consisted of bread and legumes, for there is no frivolity. He excludes a Seudah with drunkenness; it is forbidden even in a Beis Medrash. We do not protest those who are lenient about a Seudas Siyum ha'Shas, for no other place is big enough.