# Buggy code
async def send(self, request: Request, timeout: Timeout = None) -> Response:
    timeout = Timeout() if timeout is None else timeout

    if not self.init_started:
        # The very first stream is responsible for initiating the connection.
        self.init_started = True
        await self.send_connection_init(timeout)
        stream_id = self.state.get_next_available_stream_id()
        self.init_complete.set()
    else:
        # All other streams need to wait until the connection is established.
        await self.init_complete.wait()
        stream_id = self.state.get_next_available_stream_id()

    stream = HTTP2Stream(stream_id=stream_id, connection=self)
    self.streams[stream_id] = stream
    self.events[stream_id] = []
    return await stream.send(request, timeout)


# Traceback (AssertionError)
Traceback (most recent call last):
File "/Users/primoz/PycharmProjects/orange3-imageanalytics/example.py", line 57, in <module>
asyncio.get_event_loop().run_until_complete(cl.embedd_batch())
File "/Users/primoz/miniconda3/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "/Users/primoz/PycharmProjects/orange3-imageanalytics/example.py", line 19, in embedd_batch
embeddings = await asyncio.gather(*requests)
File "/Users/primoz/PycharmProjects/orange3-imageanalytics/example.py", line 43, in _send_to_server
emb = await self._send_request(client, im, url)
File "/Users/primoz/PycharmProjects/orange3-imageanalytics/example.py", line 50, in _send_request
response = await client.post(url, headers=headers, data=image)
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/client.py", line 492, in post
trust_env=trust_env,
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/client.py", line 634, in request
trust_env=trust_env,
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/client.py", line 658, in send
trust_env=trust_env,
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/client.py", line 273, in _get_response
return await get_response(request)
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/middleware/redirect.py", line 31, in __call__
response = await get_response(request)
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/client.py", line 228, in get_response
request, verify=verify, cert=cert, timeout=timeout
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/dispatch/connection_pool.py", line 126, in send
raise exc
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/dispatch/connection_pool.py", line 121, in send
request, verify=verify, cert=cert, timeout=timeout
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/dispatch/connection.py", line 62, in send
response = await self.h2_connection.send(request, timeout=timeout)
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/dispatch/http2.py", line 49, in send
stream_id = await self.send_headers(request, timeout)
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/dispatch/http2.py", line 121, in send_headers
await self.stream.write(data_to_send, timeout)
File "/Users/primoz/venv/orange/lib/python3.7/site-packages/httpx/concurrency/asyncio.py", line 149, in write
self.stream_writer.drain(), timeout.write_timeout
File "/Users/primoz/miniconda3/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
return fut.result()
File "/Users/primoz/miniconda3/lib/python3.7/asyncio/streams.py", line 348, in drain
await self._protocol._drain_helper()
File "/Users/primoz/miniconda3/lib/python3.7/asyncio/streams.py", line 206, in _drain_helper
assert waiter is None or waiter.cancelled()
AssertionError

# Fixed code
async def send(self, request: Request, timeout: Timeout = None) -> Response:
    timeout = Timeout() if timeout is None else timeout

    async with self.init_lock:
        if not self.sent_connection_init:
            # The very first stream is responsible for initiating the connection.
            await self.send_connection_init(timeout)
            self.sent_connection_init = True
        stream_id = self.state.get_next_available_stream_id()

    stream = HTTP2Stream(stream_id=stream_id, connection=self)
    self.streams[stream_id] = stream
    self.events[stream_id] = []
    return await stream.send(request, timeout)