Based on this question I would like to know what the earliest source for fasting when a sefer torah falls comes from? Is is true that if a person is in the room but turns away so as not to see it fall, that that person is not obligated to fast? Where does the oft quoted '40 days' come from (I would assume it is related to kabbalas hatorah, but I have no source)?
The earliest source is the Shu"t Mishpetei Shmuel 12 quoted by the Magen Avraham 44:5. He bases it on the gemara in Moed Katan 26a that says you need to fast if you see a sefer Torah burnt in your dream. The Igros Mosheh OC 3:3 holds that according to halachah, most authorities hold that only those who drop it need to fast, but there is a minhag for those who see it to fast as well.