# Buggy code
async def request(
    loop: asyncio.AbstractEventLoop,
    address: str,
    services: List[str],
    port: int = 5353,
    timeout: int = 4,
):
    """Send request for services to a host."""
    if not net.get_local_address_reaching(IPv4Address(address)):
        raise exceptions.NonLocalSubnetError(
            f"address {address} is not in any local subnet"
        )

    transport, protocol = await loop.create_datagram_endpoint(
        lambda: UnicastDnsSdClientProtocol(loop, services, address),
        remote_addr=(address, port),
    )

    try:
        return await cast(UnicastDnsSdClientProtocol, protocol).get_response(timeout)
    finally:
        transport.close()


# Traceback (asyncio.TimeoutError)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 565, in _run_application
return await cli_handler(loop)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 388, in cli_handler
return await _handle_commands(args, config, loop)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 480, in _handle_commands
ret = await _handle_device_command(args, cmd, atv, loop)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 517, in _handle_device_command
return (await _exec_command(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/__main__.py", line 531, in _exec_command
value = await tmp(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/__init__.py", line 71, in play_url
player = await self._player(session)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/__init__.py", line 51, in _player
await verifier.verify_authed()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/auth.py", line 88, in verify_authed
resp = await self._send(self.srp.verify1(), 'verify1')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/airplay/auth.py", line 100, in _send
resp, code = await self.http.post_data(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/net.py", line 76, in post_data
raise ex
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pyatv/net.py", line 64, in post_data
resp = await self._session.post(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/client.py", line 504, in _request
await resp.start(conn)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 860, in start
self._continue = None
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/helpers.py", line 596, in __exit__
raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError

# Fixed code
async def request(
    loop: asyncio.AbstractEventLoop,
    address: str,
    services: List[str],
    port: int = 5353,
    timeout: int = 4,
):
    """Send request for services to a host."""
    if not net.get_local_address_reaching(IPv4Address(address)):
        raise exceptions.NonLocalSubnetError(
            f"address {address} is not in any local subnet"
        )

    transport, protocol = await loop.create_datagram_endpoint(
        lambda: UnicastDnsSdClientProtocol(loop, services, address, timeout),
        remote_addr=(address, port),
    )

    try:
        return await cast(UnicastDnsSdClientProtocol, protocol).get_response()
    finally:
        transport.close()