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

Conversation

fydai
Copy link
Contributor

@fydai fydai commented Dec 15, 2019

We are currently accessing the say function from multiple threads (celery, web server, and standard server) all use it. Adding a lock would remove any possible synchronization issues from using multiple threads.

@@ -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.

@cg505
Copy link
Member

cg505 commented Dec 17, 2019

We should lock other interactions that use the IRC connection, like changing the topic.

@dkess
Copy link
Member

dkess commented Dec 18, 2019

By all means look into it, but if we let perfect be the enemy of the good here the IRC bot will remain stagnant forever. I would argue that any effort spent on getting the synchronization exactly right is better directed towards refactoring the bot entirely.

Copy link
Member

@cg505 cg505 left a comment

Choose a reason for hiding this comment

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

fair enough, I'll open an issue for locking other areas.

@nattofriends
Copy link
Member

We should lock other interactions that use the IRC connection, like changing the topic.

This needs a lock because multiple messages are being sent. Changing the topic doesn't create multiple messages.

Comment on lines +348 to +354
# 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)

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

@cg505
Copy link
Member

cg505 commented Dec 29, 2019

The lock should be held whenever the socket needs to be used (that is, whenever we interact with the IRC server) because sockets in Python are not thread safe.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants