The Messenger of Allah ﷺ said, "Whoever sits in a gathering in which much idle talk takes place, then before rising from that gathering says, 'Glory be to You, O Allah, and praise be to You. I bear witness that there is no god but You. I seek Your forgiveness and turn to You in repentance,' Allah will forgive him for whatever occurred in that gathering."
حدثنا أبو عبيدة بن أبي السفر الكوفي واسمه أحمد بن عبد الله الهمداني حدثنا الحجاج بن محمد قال قال ابن جريج أخبرني موسى بن عقبة عن سهيل بن أبي صالح عن أبيه
قال رسول الله ﷺ من جلس في مجلس فكثر فيه لغطه فقال قبل أن يقوم من مجلسه ذلك سبحانك اللهم وبحمدك أشهد أن لا إله إلا أنت أستغفرك وأتوب إليك إلا غفر له ما كان في مجلسه ذلك
[Sahih]. Nasai narrated it in Amal al-Yawm wal-Layla, no. 397 (duplicate), through the hadith of Hajjaj bin Muhammad with the same wording. Ibn Hibban graded it sahih, no. 2366, as did Hakim, 1/536-537, and Dhahabi agreed with him. Bukhari, Abu Zura, Abu Hatim, and Daraqutni found fault with it due to a flaw that does not detract from its soundness, and Allah knows best. It has another route in Ibn Hibban, no. 3670.On this topic there is also a narration from Abu Barza [Abu Dawud, no. 4859, and Hakim, 1/537] and from Ayesha [Nasai in Amal al-Yawm wal-Layla, no. 398].