self.remove_handler(fd)
if all_fds:
self.close_fd(fileobj)
- self.asyncio_loop.close()
+ # Remove the mapping before closing the asyncio loop. If this
+ # happened in the other order, we could race against another
+ # initialize() call which would see the closed asyncio loop,
+ # assume it was closed from the asyncio side, and do this
+ # cleanup for us, leading to a KeyError.
del IOLoop._ioloop_for_asyncio[self.asyncio_loop]
+ self.asyncio_loop.close()
def add_handler(self, fd, handler, events):
fd, fileobj = self.split_fd(fd)
client.close()
server.close()
+ @gen_test
+ def test_init_close_race(self):
+ # Regression test for #2367
+ def f():
+ for i in range(10):
+ loop = IOLoop()
+ loop.close()
+
+ yield gen.multi([self.io_loop.run_in_executor(None, f) for i in range(2)])
+
# Deliberately not a subclass of AsyncTestCase so the IOLoop isn't
# automatically set as current.