diff --git a/aiohttp/file_sender.py b/aiohttp/file_sender.py index 1fc53732c46..428a9f31afb 100644 --- a/aiohttp/file_sender.py +++ b/aiohttp/file_sender.py @@ -39,7 +39,7 @@ def _sendfile_cb(self, fut, out_fd, in_fd, offset, @asyncio.coroutine def _sendfile_system(self, req, resp, fobj, count): """ - Write `count` bytes of `fobj` to `resp` starting from `offset` using + Write `count` bytes of `fobj` to `resp` using the ``sendfile`` system call. `req` should be a :obj:`aiohttp.web.Request` instance. @@ -48,8 +48,6 @@ def _sendfile_system(self, req, resp, fobj, count): `fobj` should be an open file object. - `offset` should be an integer >= 0. - `count` should be an integer > 0. """ transport = req.transport @@ -61,13 +59,18 @@ def _sendfile_system(self, req, resp, fobj, count): yield from resp.drain() loop = req.app.loop - out_fd = transport.get_extra_info("socket").fileno() + # See https://github.com/KeepSafe/aiohttp/issues/958 for details + out_socket = transport.get_extra_info("socket").dup() + out_fd = out_socket.fileno() in_fd = fobj.fileno() fut = create_future(loop) - self._sendfile_cb(fut, out_fd, in_fd, 0, count, loop, False) + try: + self._sendfile_cb(fut, out_fd, in_fd, 0, count, loop, False) - yield from fut + yield from fut + finally: + out_socket.close() @asyncio.coroutine def _sendfile_fallback(self, req, resp, fobj, count):