while self._events:
fd, events = self._events.popitem()
try:
- self._handlers[fd][1](fd, events)
+ fd_obj, handler_func = self._handlers[fd]
+ handler_func(fd_obj, events)
except (OSError, IOError) as e:
if e.args[0] == errno.EPIPE:
# Happens when the client closes the connection
self.handle_callback_exception(self._handlers.get(fd))
except Exception:
self.handle_callback_exception(self._handlers.get(fd))
+ fd_obj = handler_func = None
finally:
# reset the stopped flag so another start/stop pair can be issued
def doRead(self):
if not self.lost:
- self.handler(self.fd, tornado.ioloop.IOLoop.READ)
+ self.handler(self.fileobj, tornado.ioloop.IOLoop.READ)
def doWrite(self):
if not self.lost:
- self.handler(self.fd, tornado.ioloop.IOLoop.WRITE)
+ self.handler(self.fileobj, tornado.ioloop.IOLoop.WRITE)
def connectionLost(self, reason):
if not self.lost:
- self.handler(self.fd, tornado.ioloop.IOLoop.ERROR)
+ self.handler(self.fileobj, tornado.ioloop.IOLoop.ERROR)
self.lost = True
def logPrefix(self):
io_loop.close(all_fds=True)
self.assertTrue(socket_wrapper.closed)
+ def test_handler_callback_file_object(self):
+ """The handler callback receives the same fd object it passed in."""
+ server_sock, port = bind_unused_port()
+ fds = []
+ def handle_connection(fd, events):
+ fds.append(fd)
+ conn, addr = server_sock.accept()
+ conn.close()
+ self.stop()
+ self.io_loop.add_handler(server_sock, handle_connection, IOLoop.READ)
+ with contextlib.closing(socket.socket()) as client_sock:
+ client_sock.connect(('127.0.0.1', port))
+ self.wait()
+ self.io_loop.remove_handler(server_sock)
+ self.io_loop.add_handler(server_sock.fileno(), handle_connection,
+ IOLoop.READ)
+ with contextlib.closing(socket.socket()) as client_sock:
+ client_sock.connect(('127.0.0.1', port))
+ self.wait()
+ self.assertIs(fds[0], server_sock)
+ self.assertIs(fds[1], server_sock.fileno())
+ self.io_loop.remove_handler(server_sock.fileno())
+ server_sock.close()
+
+
# Deliberately not a subclass of AsyncTestCase so the IOLoop isn't
# automatically set as current.