Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Add a lock to the bot's say function #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions ircbot/ircbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def __init__(
self.plugins: Dict[str, ModuleType] = {}
self.extra_channels: Set[str] = set() # plugins can add stuff here

# As we use threads, we should ensure that we use them safely
self.lock = threading.RLock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use an RLock here? It doesn't look to me like this lock will ever be taken twice in the same thread.


# Register plugins before joining the server.
self.register_plugins()

Expand Down Expand Up @@ -340,17 +343,19 @@ def plusone(m):
self.connection.topic(channel, new_topic=new_topic)

def say(self, channel, message):
# Find the length of the full message
msg_len = len(f'PRIVMSG {channel} :{message}\r\n'.encode('utf-8'))

# The message must be split up if over the length limit
if msg_len > MAX_CLIENT_MSG:
messages = split_utf8(message.encode('utf-8'), MAX_CLIENT_MSG)

for msg in messages:
self.connection.privmsg(channel, msg)
else:
self.connection.privmsg(channel, message)
# This is writing to a socket, it should be synchronized
with self.lock:
# Find the length of the full message
msg_len = len(f'PRIVMSG {channel} :{message}\r\n'.encode('utf-8'))

# The message must be split up if over the length limit
if msg_len > MAX_CLIENT_MSG:
messages = split_utf8(message.encode('utf-8'), MAX_CLIENT_MSG)

Comment on lines +348 to +354
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This math part doesn't need the lock. Only sending multiple messages needs the lock.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

for msg in messages:
self.connection.privmsg(channel, msg)
else:
self.connection.privmsg(channel, message)


# Generator which splits the unicode message string
Expand Down