postlund / pyatv

A client library for Apple TV and AirPlay devices
https://pyatv.dev
MIT License
841 stars 91 forks source link

TimeoutError: no response to SETUP (RTSP/1.0) #2132

Open moraespc opened 11 months ago

moraespc commented 11 months ago

Describe the bug

Hi everyone,

I'm encountering an issue that I could use some help with. My code is designed to work with multiple AppleTV devices, and usually, it performs as expected. However, when I set up the code with around 4 devices or more, all at the same network and with Airplay permissions allowed properly, it begins working correctly. But, here's the problem: if one of the devices turns off due to something like running out of power, I receive an error message (attached), and the entire functionality breaks down. I've noticed that exception occurs on atv "connect" method. The only solution I've found was delete saved credentials and restart the entire code (including pairing process).

Could this be a bug in the system?

I've experimented by removing the AirPlay protocol credentials and the exception no longer occurs. However, this workaround comes at the cost of losing certain functionalities like Artwork and Hold Action.

Any insights or suggestions would be greatly appreciated. Thanks in advance!

Error log

Failed to set up remote control channel
Traceback (most recent call last):
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\support\http.py", line 463, in send_and_receive
    await pending_request.event.wait()
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\asyncio\locks.py", line 213, in wait
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\support\http.py", line 462, in send_and_receive
    async with async_timeout.timeout(timeout):
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\async_timeout\__init__.py", line 129, in __aexit__
    self._do_exit(exc_type)
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\async_timeout\__init__.py", line 212, in _do_exit
    raise asyncio.TimeoutError
TimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\protocols\airplay\__init__.py", line 350, in _connect_rc
    await session.setup_remote_control()
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\protocols\airplay\ap2_session.py", line 73, in setup_remote_control
    await self._setup_event_channel(self.connection.remote_ip)
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\protocols\airplay\ap2_session.py", line 112, in _setup_event_channel
    resp = await self._setup(
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\protocols\airplay\ap2_session.py", line 105, in _setup
    resp = await self.rtsp.setup(body=body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\support\rtsp.py", line 176, in setup
    return await self.exchange("SETUP", headers=headers, body=body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\support\rtsp.py", line 293, in exchange
    resp = await self.connection.send_and_receive(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Paulo\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyatv\support\http.py", line 474, in send_and_receive
    raise TimeoutError(f"no response to {method} {uri} ({protocol})") from ex
TimeoutError: no response to SETUP rtsp://192.168.10.121/1614960693 (RTSP/1.0)

PS: 192.168.10.121 is the local machine IP Address

How to reproduce the bug?

Set up more than 4 Apple TV's, controlled by the same local machine, and pull off power supply.

What is expected behavior?

All devices connecting correctly

Operating System

Windows 10

Python

Other

pyatv

0.13.4

Device

Apple TV 4k (3rd gen) tvOS 16.6

Additional context

Python 3.11

postlund commented 11 months ago

Interesting. I just want to make sure that I get this right:

moraespc commented 10 months ago

I apologize for the delay in responding. I was doing as many tests as possible to get consistent answers regarding the problem since the devices are not exactly in my house. I tried reproduce the error using multiple instances of virtual devices, but pairing with my only one physical device and it was very difficult for me to reproduce this exception once again. However, i've noticed something that can be helpful. If you pair with Apple TV (devices shows up at App Remote & Devices list) and shortly thereafter remove power supply and put it back, the devices will be no longer listed as allowed, and this exactly exception occurs. That wasn't the way it occurred at the first time so I'm worried to say that the problem is exactly that. Do you have any clue about Apple removing paired devices for any reason or if exists a time after pairing that it takes to be "stored" on device's memory?

Finally, answering the questions: