# Buggy code
async def sendfile(self) -> None:
    assert self.transport is not None
    loop = self.loop
    data = b"".join(self._sendfile_buffer)
    if hasattr(loop, "sendfile"):
        # Python 3.7+
        self.transport.write(data)
        await loop.sendfile(self.transport, self._fobj, self._offset, self._count)
        await super().write_eof()
        return

    self._fobj.seek(self._offset)
    out_socket = self.transport.get_extra_info("socket").dup()
    out_socket.setblocking(False)
    out_fd = out_socket.fileno()

    try:
        await loop.sock_sendall(out_socket, data)
        if not self._do_sendfile(out_fd):
            fut = loop.create_future()
            fut.add_done_callback(partial(self._done_fut, out_fd))
            loop.add_writer(out_fd, self._sendfile_cb, fut, out_fd)
            await fut
    except asyncio.CancelledError:
        raise
    except Exception:
        server_logger.debug("Socket error")
        self.transport.close()
    finally:
        out_socket.close()

    await super().write_eof()


# Traceback (ValueError)
Traceback (most recent call last):
File "/__w/core/core/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 461, in start
resp, reset = await task
File "/__w/core/core/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 414, in _handle_request
reset = await self.finish_response(request, resp, start_time)
File "/__w/core/core/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 568, in finish_response
await prepare_meth(request)
File "/__w/core/core/venv/lib/python3.7/site-packages/aiohttp/web_fileresponse.py", line 373, in prepare
return await self._sendfile(request, fobj, offset, count)
File "/__w/core/core/venv/lib/python3.7/site-packages/aiohttp/web_fileresponse.py", line 191, in _sendfile_system
await writer.sendfile()
File "/__w/core/core/venv/lib/python3.7/site-packages/aiohttp/web_fileresponse.py", line 107, in sendfile
self._count
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 1044, in sendfile
offset, count)
File "/usr/local/lib/python3.7/asyncio/selector_events.py", line 551, in _sendfile_native
fallback=False)
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 788, in sock_sendfile
self._check_sendfile_params(sock, file, offset, count)
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 842, in _check_sendfile_params
"count must be a positive integer (got {!r})".format(count))
ValueError: count must be a positive integer (got 0)

# Fixed code
async def sendfile(self) -> None:
    assert self.transport is not None
    loop = self.loop
    data = b"".join(self._sendfile_buffer)
    if hasattr(loop, "sendfile"):
        # Python 3.7+
        self.transport.write(data)
        if self._count != 0:
            await loop.sendfile(self.transport, self._fobj, self._offset, self._count)
        await super().write_eof()
        return

    self._fobj.seek(self._offset)
    out_socket = self.transport.get_extra_info("socket").dup()
    out_socket.setblocking(False)
    out_fd = out_socket.fileno()

    try:
        await loop.sock_sendall(out_socket, data)
        if not self._do_sendfile(out_fd):
            fut = loop.create_future()
            fut.add_done_callback(partial(self._done_fut, out_fd))
            loop.add_writer(out_fd, self._sendfile_cb, fut, out_fd)
            await fut
    except asyncio.CancelledError:
        raise
    except Exception:
        server_logger.debug("Socket error")
        self.transport.close()
    finally:
        out_socket.close()

    await super().write_eof()