From: Ben Darnell Date: Fri, 7 Dec 2012 19:19:48 +0000 (-0500) Subject: Catch all exceptions, not just {OS,IO}Error in IOLoop.remove_handler. X-Git-Tag: v3.0.0~203 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6593a38c08ea366c60c50e086f2c600ffa56179;p=thirdparty%2Ftornado.git Catch all exceptions, not just {OS,IO}Error in IOLoop.remove_handler. Attempting to remove a non-existent fd raises IOError on epoll but KeyError on kqueue; this change swallows any exeption to make both platforms consistent. --- diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 3c9b05b9a..1db5e5a93 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -442,7 +442,7 @@ class PollIOLoop(IOLoop): self._events.pop(fd, None) try: self._impl.unregister(fd) - except (OSError, IOError): + except Exception: gen_log.debug("Error deleting fd from IOLoop", exc_info=True) def set_blocking_signal_threshold(self, seconds, action): diff --git a/tornado/platform/twisted.py b/tornado/platform/twisted.py index 6c3cbf967..4536837f3 100644 --- a/tornado/platform/twisted.py +++ b/tornado/platform/twisted.py @@ -431,6 +431,8 @@ class TwistedIOLoop(tornado.ioloop.IOLoop): self.reactor.removeWriter(self.fds[fd]) def remove_handler(self, fd): + if fd not in self.fds: + return self.fds[fd].lost = True if self.fds[fd].reading: self.reactor.removeReader(self.fds[fd]) diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index 569b03ca5..fa403c0c7 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -65,6 +65,15 @@ class TestIOLoop(AsyncTestCase): self.io_loop.remove_handler(sock.fileno()) sock.close() + def test_remove_without_add(self): + # remove_handler should not throw an exception if called on an fd + # was never added. + sock, port = bind_unused_port() + try: + self.io_loop.remove_handler(sock.fileno()) + finally: + sock.close() + def test_add_callback_from_signal(self): # cheat a little bit and just run this normally, since we can't # easily simulate the races that happen with real signal handlers