Skip to content

Commit

Permalink
Fixed reentrancy in tell_rooms/on_msg
Browse files Browse the repository at this point in the history
  • Loading branch information
quartata committed Dec 5, 2017
1 parent 8f20c74 commit 3058f36
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
48 changes: 27 additions & 21 deletions chatcommunicate.py
Expand Up @@ -5,6 +5,7 @@
import itertools
import os.path
import pickle
import queue
import regex
import sys
import threading
Expand Down Expand Up @@ -49,6 +50,7 @@ class CmdException(Exception):
_global_block = -1
_rooms = {}
_last_messages = LastMessages({}, collections.OrderedDict())
_msg_queue = queue.Queue()

_pickle_run = threading.Event()

Expand Down Expand Up @@ -92,6 +94,7 @@ def init(username, password):
_last_messages = pickle.load(open("messageData.p", "rb"))

threading.Thread(name="pickle ---rick--- runner", target=pickle_last_messages, daemon=True).start()
threading.Thread(name="message sender", target=send_messages, daemon=True).start()


def parse_room_config(path):
Expand Down Expand Up @@ -130,6 +133,15 @@ def pickle_last_messages():
pickle.dump(_last_messages, pickle_file)


def send_messages():
while True:
room, msg = _msg_queue.get()

room.lock.wait()
room.lock.clear()
room.room._client._do_action_despite_throttling(("send", room.room.id, msg))


def on_msg(msg, client):
if isinstance(msg, events.MessagePosted) or isinstance(msg, events.MessageEdited):
message = msg.message
Expand Down Expand Up @@ -181,26 +193,6 @@ def on_msg(msg, client):
message.reply(result, length_check=False)


def send_to_room(room, msg, report_data=()):
timestamp = time.time()

if room.block_time < timestamp and _global_block < timestamp:
msg = msg.rstrip()

if report_data:
room.last_report_data = report_data

if "delay" in _room_roles and room in _room_roles["delay"]:
threading.Thread(name="delayed post",
target=DeletionWatcher.post_message_if_not_deleted,
args=(report_data[0], msg, room))
return

room.lock.wait()
room.lock.clear()
room.room.send_message(msg)


def tell_rooms_with(prop, msg, notify_site="", report_data=()):
tell_rooms(msg, (prop,), (), notify_site=notify_site, report_data=report_data)

Expand Down Expand Up @@ -241,7 +233,21 @@ def tell_rooms(msg, has, hasnt, notify_site="", report_data=()):

msg = datahandling.append_pings(msg, pings)

send_to_room(room, msg, report_data=report_data)
timestamp = time.time()

if room.block_time < timestamp and _global_block < timestamp:
msg = msg.rstrip()

if report_data:
room.last_report_data = report_data

if "delay" in _room_roles and room in _room_roles["delay"]:
threading.Thread(name="delayed post",
target=DeletionWatcher.post_message_if_not_deleted,
args=(report_data[0], msg, room))
continue

_msg_queue.put((room, msg))


def get_last_messages(room, count):
Expand Down
4 changes: 1 addition & 3 deletions deletionwatcher.py
Expand Up @@ -98,6 +98,4 @@ def post_message_if_not_deleted(self, post_url, message_text, room):
if not was_report_deleted and not datahandling.is_false_positive(post_site_id[0:2]) and not \
datahandling.is_ignored_post(post_site_id[0:2]):

room.lock.wait()
room.lock.clear()
room.room.send_message(message_text)
chatcommunicate._msg_queue.put((room, message_text))

0 comments on commit 3058f36

Please sign in to comment.