# Buggy code
async def send(self, message):
    global DEFAULT_HEADERS

    protocol = self.protocol
    message_type = message["type"]

    if not protocol.writable:
        await protocol.writable_event.wait()

    if not self.response_started:
        # Sending response status line and headers
        if message_type != "http.response.start":
            msg = "Expected ASGI message 'http.response.start', but got '%s'."
            raise RuntimeError(msg % message_type)

        self.response_started = True

        status_code = message["status"]
        headers = DEFAULT_HEADERS + message.get("headers", [])

        if protocol.access_logs:
            protocol.logger.info(
                '%s - "%s %s HTTP/%s" %d',
                self.scope["server"][0],
                self.scope["method"],
                self.scope["path"],
                self.scope["http_version"],
                status_code,
            )

        # Write response status line and headers
        reason = STATUS_PHRASES[status_code]
        event = h11.Response(status_code=status_code, headers=headers, reason=reason)
        output = protocol.conn.send(event)
        protocol.transport.write(output)

    elif not self.response_complete:
        # Sending response body
        if message_type != "http.response.body":
            msg = "Expected ASGI message 'http.response.body', but got '%s'."
            raise RuntimeError(msg % message_type)

        body = message.get("body", b"")
        more_body = message.get("more_body", False)

        # Write response body
        event = h11.Data(data=body)
        output = protocol.conn.send(event)
        protocol.transport.write(output)

        # Handle response completion
        if not more_body:
            self.response_complete = True
            event = h11.EndOfMessage()
            output = protocol.conn.send(event)
            protocol.transport.write(output)

    else:
        # Response already sent
        msg = "Unexpected ASGI message '%s' sent, after response already completed."
        raise RuntimeError(msg % message_type)

    if protocol.conn.our_state is h11.MUST_CLOSE:
        event = h11.ConnectionClosed()
        protocol.conn.send(event)
        protocol.transport.close()
    elif protocol.conn.our_state is h11.DONE and protocol.conn.their_state is h11.DONE:
        protocol.resume_reading()
        protocol.conn.start_next_cycle()


# Traceback (RuntimeError)
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/uvicorn/protocols/http/httptools.py", line 196, in run_asgi
result = await asgi(self.receive, self.send)
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/channels/http.py", line 190, in __call__
await self.handle(body)
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 110, in __call__
return await asyncio.wait_for(future, timeout=None)
File "/usr/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
return (yield from fut)
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 125, in thread_handler
return self.func(*args, **kwargs)
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/channels/http.py", line 229, in handle
self.send(response_message)
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 64, in __call__
return call_result.result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/asgiref/sync.py", line 78, in main_wrap
result = await self.awaitable(*args, **kwargs)
File "/home/chillar/.virtualenvs/library/lib/python3.6/site-packages/uvicorn/protocols/http/httptools.py", line 308, in send
protocol.transport.write(body)
File "uvloop/handles/stream.pyx", line 636, in uvloop.loop.UVStream.write
File "uvloop/handles/handle.pyx", line 165, in uvloop.loop.UVHandle._ensure_alive
RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x1a48ad8>; the handler is closed

# Fixed code
async def send(self, message):
    global DEFAULT_HEADERS

    protocol = self.protocol
    message_type = message["type"]

    if self.disconnected:
        return

    if not protocol.writable:
        await protocol.writable_event.wait()

    if not self.response_started:
        # Sending response status line and headers
        if message_type != "http.response.start":
            msg = "Expected ASGI message 'http.response.start', but got '%s'."
            raise RuntimeError(msg % message_type)

        self.response_started = True

        status_code = message["status"]
        headers = DEFAULT_HEADERS + message.get("headers", [])

        if protocol.access_logs:
            protocol.logger.info(
                '%s - "%s %s HTTP/%s" %d',
                self.scope["server"][0],
                self.scope["method"],
                self.scope["path"],
                self.scope["http_version"],
                status_code,
            )

        # Write response status line and headers
        reason = STATUS_PHRASES[status_code]
        event = h11.Response(status_code=status_code, headers=headers, reason=reason)
        output = protocol.conn.send(event)
        protocol.transport.write(output)

    elif not self.response_complete:
        # Sending response body
        if message_type != "http.response.body":
            msg = "Expected ASGI message 'http.response.body', but got '%s'."
            raise RuntimeError(msg % message_type)

        body = message.get("body", b"")
        more_body = message.get("more_body", False)

        # Write response body
        event = h11.Data(data=body)
        output = protocol.conn.send(event)
        protocol.transport.write(output)

        # Handle response completion
        if not more_body:
            self.response_complete = True
            event = h11.EndOfMessage()
            output = protocol.conn.send(event)
            protocol.transport.write(output)

    else:
        # Response already sent
        msg = "Unexpected ASGI message '%s' sent, after response already completed."
        raise RuntimeError(msg % message_type)

    if protocol.conn.our_state is h11.MUST_CLOSE:
        event = h11.ConnectionClosed()
        protocol.conn.send(event)
        protocol.transport.close()
    elif protocol.conn.our_state is h11.DONE and protocol.conn.their_state is h11.DONE:
        protocol.resume_reading()
        protocol.conn.start_next_cycle()