# Buggy code
async def _stream_protocol_messages(
    self,
    protocol_class: Type[ProtocolAPI],
) -> AsyncIterator[CommandAPI[Any]]:
    """
    Stream the messages for the specified protocol.
    """
    async with self._protocol_locks.lock(protocol_class):
        msg_queue = self._protocol_queues[protocol_class]
        if not hasattr(self, "_multiplex_token"):
            raise Exception("Multiplexer is not multiplexed")
        token = self._multiplex_token

        while not self.is_closing and not token.triggered:
            try:
                # We use an optimistic strategy here of using
                # `get_nowait()` to reduce the number of times we yield to
                # the event loop.  Since this is an async generator it will
                # yield to the loop each time it returns a value so we
                # don't have to worry about this blocking other processes.
                yield msg_queue.get_nowait()
            except asyncio.QueueEmpty:
                yield await self.wait(msg_queue.get(), token=token)


# Traceback (KeyError)
DEBUG  12-18 15:21:47  async_process_runner.py  b'\x1b[1m\x1b[31m   ERROR  2019-12-18 15:21:47,268           LESPeerPool  unexpected error during peer connection\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'Traceback (most recent call last):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 209, in maybe_connect_more_peers\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    for backend in self.peer_backends\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 191, in _add_peers_from_backend\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    await self.connect_to_nodes(iter(candidates))\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 419, in connect_to_nodes\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    loop=self.get_event_loop(),\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 440, in connect_to_node\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    async with self.lock_node_for_handshake(node):\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/peer_pool.py", line 423, in lock_node_for_handshake\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self._handshake_locks.lock(node)\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/p2p/resource_lock.py", line 18, in lock\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    lock = self._locks[resource]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'  File "/home/circleci/repo/.tox/py36-wheel-cli/lib/python3.6/weakref.py", line 394, in __getitem__\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b'    return self.data[ref(key)]\n'
DEBUG  12-18 15:21:47  async_process_runner.py  b"KeyError: <weakref at 0x7f48b9d9cf48; to 'Node' at 0x7f48b83175c0>\x1b[0m\n"

# Fixed code
async def _stream_protocol_messages(
    self,
    protocol_class: Type[ProtocolAPI],
) -> AsyncIterator[CommandAPI[Any]]:
    """
    Stream the messages for the specified protocol.
    """
    async with self._protocol_locks[protocol_class]:
        msg_queue = self._protocol_queues[protocol_class]
        if not hasattr(self, "_multiplex_token"):
            raise Exception("Multiplexer is not multiplexed")
        token = self._multiplex_token

        while not self.is_closing and not token.triggered:
            try:
                # We use an optimistic strategy here of using
                # `get_nowait()` to reduce the number of times we yield to
                # the event loop.  Since this is an async generator it will
                # yield to the loop each time it returns a value so we
                # don't have to worry about this blocking other processes.
                yield msg_queue.get_nowait()
            except asyncio.QueueEmpty:
                yield await msg_queue.get()