From 36038382b2b8a2631716e03f595b567d01af961d Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 1 Feb 2017 05:16:10 +0100 Subject: [PATCH] python3: Cache async channel FDs so that updates always happen correctly. --- python3/ashd/async.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python3/ashd/async.py b/python3/ashd/async.py index aa52af9..238f6b7 100644 --- a/python3/ashd/async.py +++ b/python3/ashd/async.py @@ -5,6 +5,7 @@ class epoller(object): def __init__(self, check=None): self.registered = {} + self.fdcache = {} self.lock = threading.RLock() self.ep = None self.th = None @@ -86,6 +87,7 @@ class epoller(object): if fd in self.registered: nevs = self._evsfor(ch) if nevs == 0: + del self.fdcache[ch] del self.registered[fd] ep.unregister(fd) self._cb(ch, "close") @@ -119,6 +121,7 @@ class epoller(object): ch.close() return ch.watcher = self + self.fdcache[ch] = fd self.registered[fd] = (ch, evs) if self.ep: self.ep.register(fd, evs) @@ -126,14 +129,16 @@ class epoller(object): def remove(self, ch, ignore=False): with self.lock: - fd = ch.fileno() - if fd not in self.registered: + try: + fd = self.fdcache[ch] + except KeyError: if ignore: return raise KeyError("fd %i is not registered" % fd) pch, cevs = self.registered[fd] if pch is not ch: raise ValueError("fd %i registered via object %r, cannot remove with %r" % (pch, ch)) + del self.fdcache[ch] del self.registered[fd] if self.ep: self.ep.unregister(fd) @@ -141,8 +146,9 @@ class epoller(object): def update(self, ch, ignore=False): with self.lock: - fd = ch.fileno() - if fd not in self.registered: + try: + fd = self.fdcache[ch] + except KeyError: if ignore: return raise KeyError("fd %i is not registered" % fd) @@ -151,6 +157,7 @@ class epoller(object): raise ValueError("fd %i registered via object %r, cannot update with %r" % (pch, ch)) evs = self._evsfor(ch) if evs == 0: + del self.fdcache[ch] del self.registered[fd] if self.ep: self.ep.unregister(fd) -- 2.11.0