From 7970fca93a07f962abaa0a0c0cc0854ec6f9864c Mon Sep 17 00:00:00 2001 From: nemunaire Date: Sat, 5 Sep 2015 10:14:10 +0200 Subject: [PATCH] Use with section for locking threadsafe region (instead of raw calls to acquire/release) --- nemubot/bot.py | 82 +++++++++++++++++++------------------- nemubot/server/abstract.py | 20 +++++----- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/nemubot/bot.py b/nemubot/bot.py index f349011..777b931 100644 --- a/nemubot/bot.py +++ b/nemubot/bot.py @@ -145,52 +145,50 @@ class Bot(threading.Thread): self.stop = False while not self.stop: - _lock.acquire() - try: - rl, wl, xl = select(_rlist, _wlist, _xlist, 0.1) - except: - logger.error("Something went wrong in select") - fnd_smth = False - # Looking for invalid server - for r in _rlist: - if not hasattr(r, "fileno") or not isinstance(r.fileno(), int) or r.fileno() < 0: - _rlist.remove(r) - logger.error("Found invalid object in _rlist: " + str(r)) - fnd_smth = True - for w in _wlist: - if not hasattr(w, "fileno") or not isinstance(w.fileno(), int) or w.fileno() < 0: - _wlist.remove(w) - logger.error("Found invalid object in _wlist: " + str(w)) - fnd_smth = True - for x in _xlist: - if not hasattr(x, "fileno") or not isinstance(x.fileno(), int) or x.fileno() < 0: - _xlist.remove(x) - logger.error("Found invalid object in _xlist: " + str(x)) - fnd_smth = True - if not fnd_smth: - logger.exception("Can't continue, sorry") - self.quit() - _lock.release() - continue + with _lock: + try: + rl, wl, xl = select(_rlist, _wlist, _xlist, 0.1) + except: + logger.error("Something went wrong in select") + fnd_smth = False + # Looking for invalid server + for r in _rlist: + if not hasattr(r, "fileno") or not isinstance(r.fileno(), int) or r.fileno() < 0: + _rlist.remove(r) + logger.error("Found invalid object in _rlist: " + str(r)) + fnd_smth = True + for w in _wlist: + if not hasattr(w, "fileno") or not isinstance(w.fileno(), int) or w.fileno() < 0: + _wlist.remove(w) + logger.error("Found invalid object in _wlist: " + str(w)) + fnd_smth = True + for x in _xlist: + if not hasattr(x, "fileno") or not isinstance(x.fileno(), int) or x.fileno() < 0: + _xlist.remove(x) + logger.error("Found invalid object in _xlist: " + str(x)) + fnd_smth = True + if not fnd_smth: + logger.exception("Can't continue, sorry") + self.quit() + continue - for x in xl: - try: - x.exception() - except: - logger.exception("Uncatched exception on server exception") - for w in wl: - try: - w.write_select() - except: - logger.exception("Uncatched exception on server write") - for r in rl: - for i in r.read(): + for x in xl: try: - self.receive_message(r, i) + x.exception() except: - logger.exception("Uncatched exception on server read") + logger.exception("Uncatched exception on server exception") + for w in wl: + try: + w.write_select() + except: + logger.exception("Uncatched exception on server write") + for r in rl: + for i in r.read(): + try: + self.receive_message(r, i) + except: + logger.exception("Uncatched exception on server read") - _lock.release() # Launch new consumer threads if necessary while self.cnsr_queue.qsize() > self.cnsr_thrd_size: diff --git a/nemubot/server/abstract.py b/nemubot/server/abstract.py index ebfab7e..99d10d5 100644 --- a/nemubot/server/abstract.py +++ b/nemubot/server/abstract.py @@ -71,17 +71,15 @@ class AbstractServer(io.IOBase): """Generic close function that register the server un _{r,w,x}list in case of successful _close""" self.logger.info("Closing connection to %s", self.id) - _lock.acquire() - if not hasattr(self, "_close") or self._close(): - if self in _rlist: - _rlist.remove(self) - if self in _wlist: - _wlist.remove(self) - if self in _xlist: - _xlist.remove(self) - _lock.release() - return True - _lock.release() + with _lock: + if not hasattr(self, "_close") or self._close(): + if self in _rlist: + _rlist.remove(self) + if self in _wlist: + _wlist.remove(self) + if self in _xlist: + _xlist.remove(self) + return True return False