Skip to content

Commit

Permalink
chan_track: Ensure the hooks acquire their locks
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Jun 1, 2020
1 parent 69528c7 commit 262e5c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed handling of colons in core IRC parser
- Fix FML random URL
- Ensure hooks are triggered according to priority
- chan_track: Ensure hooks acquire the needed locks
### Removed
- twitch.py removed due to outdated API and lack of maintainer
- Remove unused run_before events/tasks
Expand Down
41 changes: 22 additions & 19 deletions plugins/core/chan_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Requires:
server_info.py
"""
import asyncio
import gc
import json
import logging
Expand All @@ -26,6 +27,8 @@

logger = logging.getLogger("cloudbot")

data_lock = asyncio.Lock()


class WeakDict(dict):
"""
Expand Down Expand Up @@ -520,7 +523,7 @@ def replace_user_data(conn, chan_data):
del chan_data.users[old_nick]


@hook.irc_raw(['353', '366'], singlethread=True, do_sieve=False)
@hook.irc_raw(['353', '366'], singlethread=True, lock=data_lock, do_sieve=False)
def on_names(conn, irc_paramlist, irc_command):
"""
:type conn: cloudbot.client.Client
Expand Down Expand Up @@ -596,7 +599,7 @@ def serialize(self, mapping, **kwargs):
return json.dumps(self._serialize(mapping), **kwargs)


@hook.permission("chanop")
@hook.permission("chanop", lock=data_lock, do_sieve=False)
def perm_check(chan, conn, nick):
"""
:type chan: str
Expand Down Expand Up @@ -687,7 +690,7 @@ def getdata_cmd(conn, chan, nick):
return web.paste(MappingSerializer().serialize(memb, indent=2))


@hook.irc_raw(['PRIVMSG', 'NOTICE'], do_sieve=False)
@hook.irc_raw(['PRIVMSG', 'NOTICE'], lock=data_lock, do_sieve=False)
def on_msg(conn, nick, user, host, irc_paramlist):
chan, *other_data = irc_paramlist

Expand All @@ -711,7 +714,7 @@ def on_msg(conn, nick, user, host, irc_paramlist):
memb.data['last_privmsg'] = time.time()


@hook.periodic(600)
@hook.periodic(600, lock=data_lock, do_sieve=False)
def clean_pms(bot):
cutoff = time.time() - 600
for conn in bot.connections.values():
Expand All @@ -728,7 +731,7 @@ def clean_pms(bot):
pass


@hook.irc_raw('JOIN', do_sieve=False)
@hook.irc_raw('JOIN', lock=data_lock, do_sieve=False)
def on_join(nick, user, host, conn, irc_paramlist):
"""
:type nick: str
Expand Down Expand Up @@ -784,7 +787,7 @@ def _parse_mode_string(modes, params, status_modes, mode_types):
return new_modes


@hook.irc_raw('MODE', do_sieve=False)
@hook.irc_raw('MODE', lock=data_lock, do_sieve=False)
def on_mode(chan, irc_paramlist, conn):
"""
:type chan: str
Expand Down Expand Up @@ -823,7 +826,7 @@ def on_mode(chan, irc_paramlist, conn):
member.sort_status()


@hook.irc_raw('PART', do_sieve=False)
@hook.irc_raw('PART', lock=data_lock, do_sieve=False)
def on_part(chan, nick, conn):
"""
:type chan: str
Expand All @@ -838,7 +841,7 @@ def on_part(chan, nick, conn):
del chan_data.users[nick]


@hook.irc_raw('KICK', do_sieve=False)
@hook.irc_raw('KICK', lock=data_lock, do_sieve=False)
def on_kick(chan, target, conn):
"""
:type chan: str
Expand All @@ -848,7 +851,7 @@ def on_kick(chan, target, conn):
on_part(chan, target, conn)


@hook.irc_raw('QUIT', do_sieve=False)
@hook.irc_raw('QUIT', lock=data_lock, do_sieve=False)
def on_quit(nick, conn):
"""
:type nick: str
Expand All @@ -862,7 +865,7 @@ def on_quit(nick, conn):
del chan.users[nick]


@hook.irc_raw('NICK', do_sieve=False)
@hook.irc_raw('NICK', lock=data_lock, do_sieve=False)
def on_nick(nick, irc_paramlist, conn):
"""
:type nick: str
Expand All @@ -889,7 +892,7 @@ def on_nick(nick, irc_paramlist, conn):
user_chans[new_nick] = user_chans.pop(nick)


@hook.irc_raw('ACCOUNT', do_sieve=False)
@hook.irc_raw('ACCOUNT', lock=data_lock, do_sieve=False)
def on_account(conn, nick, irc_paramlist):
"""
:type nick: str
Expand All @@ -899,7 +902,7 @@ def on_account(conn, nick, irc_paramlist):
get_users(conn).getuser(nick).account = irc_paramlist[0]


@hook.irc_raw('CHGHOST', do_sieve=False)
@hook.irc_raw('CHGHOST', lock=data_lock, do_sieve=False)
def on_chghost(conn, nick, irc_paramlist):
"""
:type nick: str
Expand All @@ -912,7 +915,7 @@ def on_chghost(conn, nick, irc_paramlist):
user.host = host


@hook.irc_raw('AWAY', do_sieve=False)
@hook.irc_raw('AWAY', lock=data_lock, do_sieve=False)
def on_away(conn, nick, irc_paramlist):
"""
:type nick: str
Expand All @@ -929,7 +932,7 @@ def on_away(conn, nick, irc_paramlist):
user.away_message = reason


@hook.irc_raw('352', do_sieve=False)
@hook.irc_raw('352', lock=data_lock, do_sieve=False)
def on_who(conn, irc_paramlist):
"""
:type irc_paramlist: cloudbot.util.parsers.irc.ParamList
Expand All @@ -949,7 +952,7 @@ def on_who(conn, irc_paramlist):
user.is_oper = is_oper


@hook.irc_raw('311', do_sieve=False)
@hook.irc_raw('311', lock=data_lock, do_sieve=False)
def on_whois_name(conn, irc_paramlist):
"""
:type irc_paramlist: cloudbot.util.parsers.irc.ParamList
Expand All @@ -962,7 +965,7 @@ def on_whois_name(conn, irc_paramlist):
user.realname = realname


@hook.irc_raw('330', do_sieve=False)
@hook.irc_raw('330', lock=data_lock, do_sieve=False)
def on_whois_acct(conn, irc_paramlist):
"""
:type irc_paramlist: cloudbot.util.parsers.irc.ParamList
Expand All @@ -972,7 +975,7 @@ def on_whois_acct(conn, irc_paramlist):
get_users(conn).getuser(nick).account = acct


@hook.irc_raw('301', do_sieve=False)
@hook.irc_raw('301', lock=data_lock, do_sieve=False)
def on_whois_away(conn, irc_paramlist):
"""
:type irc_paramlist: cloudbot.util.parsers.irc.ParamList
Expand All @@ -984,7 +987,7 @@ def on_whois_away(conn, irc_paramlist):
user.away_message = msg


@hook.irc_raw('312', do_sieve=False)
@hook.irc_raw('312', lock=data_lock, do_sieve=False)
def on_whois_server(conn, irc_paramlist):
"""
:type irc_paramlist: cloudbot.util.parsers.irc.ParamList
Expand All @@ -994,7 +997,7 @@ def on_whois_server(conn, irc_paramlist):
get_users(conn).getuser(nick).server = server


@hook.irc_raw('313', do_sieve=False)
@hook.irc_raw('313', lock=data_lock, do_sieve=False)
def on_whois_oper(conn, irc_paramlist):
"""
:type irc_paramlist: cloudbot.util.parsers.irc.ParamList
Expand Down

0 comments on commit 262e5c3

Please sign in to comment.