From 3f340110ea2a6775ee5db5ff6d247a249dcffff1 Mon Sep 17 00:00:00 2001
From: Leko <rexx0520@gmail.com>
Date: Fri, 14 May 2021 23:32:43 +0800
Subject: [PATCH] fix: dictionary changed size during iteration

---
 index.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/index.py b/index.py
index 192ebc9..b0ba02b 100644
--- a/index.py
+++ b/index.py
@@ -131,13 +131,17 @@ def get_channel_name_by_video_id(video_id):
         def clear_chat():
             utils.log(f" Running chat instance clearing task.")
             global chats
+            to_del = []
             for video_id in chats:
                 if chats[video_id].is_finished():
                     if const.CHAT_CALLBACK_AFTER_EXPIRY:
                         channel_name = get_channel_name_by_video_id(video_id)
                         chat_callback.callback(chats[video_id], channel_name=channel_name, video_id=video_id)
-                    chats.pop(video_id)
-                    utils.log(f" Chat instance {video_id} has been cleared.")
+                    to_del.append(video_id)
+                    utils.log(f" Chat instance {video_id} has been queued to be cleared.")
+            for video_id in to_del:
+                chats.pop(video_id)
+                utils.log(f" Chat instance {video_id} has been cleared.")
             
         chat_expiry_task = utils.RepeatedTimer(const.CHAT_TASK_CLEAR_INTERVAL, clear_chat)