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

Add overly-safe synchronization #159

Closed
wants to merge 1 commit into from
Closed

Add overly-safe synchronization #159

wants to merge 1 commit into from

Conversation

fydai
Copy link
Contributor

@fydai fydai commented Nov 5, 2019

To be safe, we synchronize pretty much everything (responding to messages, timer-triggered "bot.say", etc). This should make it safe to perform bot methods (such as "say") from multiple threads (in particular, the webserver and the timer thread).

Copy link
Member

@kkuehlz kkuehlz left a comment

Choose a reason for hiding this comment

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

This seems like a step in the right direction, but there is a lot wrong. We are locking way more methods than necessary. We should lock only the shared resources. Only the webserver thread is going to ever call register_plugins, for example. The ircbot thread will only call on_welcome, on_pubmsg, on_currenttopic, etc.

The bot object gets shared in every thread. After a cursory glance only the bot.say method is a problem because multiple threads could write to that socket. Having things that we only use in one thread and making them part of the bot object is also not good. We do this with bot.tasks, but that is only used in the account creation thread.

I think it would be worth exploring async, and having parts of the code written with threads in mind. The IRC library we use now supports async. Right now this solution is not good because it is brittle if some semantics in the threads change in the future and it synchronizes methods that don't need to be.

@fydai
Copy link
Contributor Author

fydai commented Nov 5, 2019

Sure, we should only lock the shared resources, but I contend that we should just lock everything. It's not as elegant, there will be a completely negligible performance impact, but it will let me feel safe merging useful features like #158. If you would like to clearly identify all shared resources between the threads, and lock solely those resources, that would be much appreciated.

I also disagree that it's "brittle", because it's overly safe. There only bad thing we can do is by causing deadlock by running a synchronized function endlessly (which we don't do). Python has its GIL, which enforces that only one thread can do anything at a time. This code just merely ensures that the only thread switching can occur when the bot is done processing its current thing.

I do agree it's worth exploring async, but that would require almost a complete rewrite of ircbot, something which is very much not a priority.

@kkuehlz
Copy link
Member

kkuehlz commented Nov 5, 2019 via email

@fydai fydai closed this Dec 15, 2019
@fydai fydai deleted the threading branch December 15, 2019 05:59
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.

2 participants