Fix inundation concurrency issues

This commit is contained in:
Yohann D'ANELLO 2021-01-09 20:13:02 +01:00
parent eb97a47a25
commit ed00fd73d1
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 18 additions and 11 deletions

View File

@ -454,7 +454,7 @@ class WarningTLV(TLV):
def handle(self, user: Any, sender: Any) -> None:
user.add_message(f"warning: *A client warned you: {self.message}*"
if not user.squinnondation.no_markdown else
f"warning: A client warned you: {self.message}")
"warning: A client warned you: {self.message}")
@staticmethod
def construct(message: str) -> "WarningTLV":

View File

@ -662,14 +662,17 @@ class User(Peer):
"""
Remove messages which are overdue (older than 2 minutes) from the inundation dictionnary.
"""
self.data_lock.acquire()
for key in self.recent_messages:
if time.time() - self.recent_messages[key][1] > 120:
self.recent_messages.pop(key)
self.data_lock.release()
def main_inundation(self) -> None:
"""
The main inundation function.
"""
self.data_lock.acquire()
for key in self.recent_messages:
k = list(self.recent_messages[key][2].keys())
for key2 in k:
@ -691,6 +694,7 @@ class User(Peer):
peer = self.recent_messages[key][2][key2][0]
self.send_packet(peer, pkt)
self.recent_messages[key][2].pop(key2)
self.data_lock.release()
def add_system_message(self, msg: str, italic: bool = True, ignore_debug: bool = False) -> None:
"""
@ -1157,6 +1161,7 @@ class Inondator(Thread):
def run(self) -> None:
while True:
try:
# clean the dictionnary
if time.time() - self.last_check > 30:
self.user.clean_inundation()
@ -1168,6 +1173,8 @@ class Inondator(Thread):
self.user.refresh_history()
self.user.refresh_input()
self.user.refresh_emoji_pad()
except Exception as e:
self.user.add_system_message(f"An error occured while inondating: {e}", ignore_debug=True)
# Avoid infinite loops
time.sleep(1)