From c41d04b59e496eab453f560c003d98f932dce59c Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 9 Aug 2012 22:25:19 -0400 Subject: [PATCH] Actually remove entries properly; resolve a potential race condition. --- earwigbot/config/permissions.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/earwigbot/config/permissions.py b/earwigbot/config/permissions.py index 47762cc..24d4073 100644 --- a/earwigbot/config/permissions.py +++ b/earwigbot/config/permissions.py @@ -68,29 +68,31 @@ class PermissionsDB(object): def _set_rank(self, user, rank): """Add a User to the database under a given rank.""" - try: - self._data[rank].append(user) - except KeyError: - self._data[rank] = [user] query = "INSERT INTO users VALUES (?, ?, ?, ?)" - with sqlite.connect(self._dbfile) as conn, self._db_access_lock: - conn.execute(query, (user.nick, user.ident, user.host, rank)) + with self._db_access_lock: + with sqlite.connect(self._dbfile) as conn: + conn.execute(query, (user.nick, user.ident, user.host, rank)) + try: + self._data[rank].append(user) + except KeyError: + self._data[rank] = [user] return user def _del_rank(self, user, rank): """Remove a User from the database.""" query = """DELETE FROM users WHERE user_nick = ? AND user_ident = ? AND user_host = ? AND user_rank = ?""" - try: - for rule in self._data[rank]: - if user in rule: - with self._db_access_lock: + with self._db_access_lock: + try: + for rule in self._data[rank]: + if user in rule: with sqlite.connect(self._dbfile) as conn: args = (user.nick, user.ident, user.host, rank) conn.execute(query, args) - return rule - except KeyError: - pass + self._data[rank].remove(user) + return rule + except KeyError: + pass return None @property