Skip to content

Commit

Permalink
Fix for #958: dup a socket for sendfile usage (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov authored Jul 14, 2016
1 parent b9240ae commit 62536ef
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions aiohttp/file_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 62536ef

Please sign in to comment.