Fiqh-us-Sunnah Volume 4, Funerals and Dhikr, Fiqh 4.107A.

Section : Atonement for Sitting in an Assembly.

Abu Hurairah reported that the Prophet, peace be upon him, said, “If anyone sits in an assembly where there is much clamour and says before getting up to leave, Subhanaka Allahumma wa bihamdika, ashadu an-la illaha illa-anta, astaghfiruka wa atubu ilayka (Glory be to You, O Allah, and I begin with declaring all praise is due to You, I testify that there is no god but You; I ask Your pardon and turn to You in repentance), he will be forgiven any sin that he might have committed while in that assembly. (Tirmizhi and Al-Baihaqi, (Kitab ad-D’wat Al-kabir))