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

@ -453,8 +453,8 @@ class WarningTLV(TLV):
def handle(self, user: Any, sender: Any) -> None: def handle(self, user: Any, sender: Any) -> None:
user.add_message(f"warning: *A client warned you: {self.message}*" user.add_message(f"warning: *A client warned you: {self.message}*"
if not user.squinnondation.no_markdown else if not user.squinnondation.no_markdown else
f"warning: A client warned you: {self.message}") "warning: A client warned you: {self.message}")
@staticmethod @staticmethod
def construct(message: str) -> "WarningTLV": 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. Remove messages which are overdue (older than 2 minutes) from the inundation dictionnary.
""" """
self.data_lock.acquire()
for key in self.recent_messages: for key in self.recent_messages:
if time.time() - self.recent_messages[key][1] > 120: if time.time() - self.recent_messages[key][1] > 120:
self.recent_messages.pop(key) self.recent_messages.pop(key)
self.data_lock.release()
def main_inundation(self) -> None: def main_inundation(self) -> None:
""" """
The main inundation function. The main inundation function.
""" """
self.data_lock.acquire()
for key in self.recent_messages: for key in self.recent_messages:
k = list(self.recent_messages[key][2].keys()) k = list(self.recent_messages[key][2].keys())
for key2 in k: for key2 in k:
@ -691,6 +694,7 @@ class User(Peer):
peer = self.recent_messages[key][2][key2][0] peer = self.recent_messages[key][2][key2][0]
self.send_packet(peer, pkt) self.send_packet(peer, pkt)
self.recent_messages[key][2].pop(key2) 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: def add_system_message(self, msg: str, italic: bool = True, ignore_debug: bool = False) -> None:
""" """
@ -1157,17 +1161,20 @@ class Inondator(Thread):
def run(self) -> None: def run(self) -> None:
while True: while True:
# clean the dictionnary try:
if time.time() - self.last_check > 30: # clean the dictionnary
self.user.clean_inundation() if time.time() - self.last_check > 30:
self.last_check = time.time() self.user.clean_inundation()
self.last_check = time.time()
# inundate # inundate
self.user.main_inundation() self.user.main_inundation()
self.user.refresh_history() self.user.refresh_history()
self.user.refresh_input() self.user.refresh_input()
self.user.refresh_emoji_pad() 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 # Avoid infinite loops
time.sleep(1) time.sleep(1)