bsharper / atv-desktop-remote

A simple app to allow you to control an Apple TV from your desktop
MIT License
255 stars 17 forks source link

Bug: A stale wsserver.py prevents new app from opening #33

Open jklewa opened 6 days ago

jklewa commented 6 days ago

System

MacOS Sequoia 15.1 (24B83) arm64

Issue Description

When attempting to (re)open the ATV Remote app, the main window is briefly visible but then the app crashes/closes.

ps aux | grep "ATV" showed several stale processes still running, interfering with the new app's startup.

Workaround

Stale processes

the347           11467   0.0  0.2 412581424  53088   ??  S     1:12PM   0:02.99 /Applications/ATV Remote.app/Contents/Frameworks/ATV Remote Helper (GPU).app/Contents/MacOS/ATV Remote Helper (GPU) --type=gpu-process --field-trial-handle=1718379636,488578797432545079,6026224567748839752,131072 --disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies,SpareRendererForSitePerProcess,WindowCaptureMacV2 --gpu-preferences=SAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADYAwAAAAAAANgDAAAAAAAASAEAACgAAABAAQAAAAAAAEgBAAAAAAAAUAEAAAAAAABYAQAAAAAAAGABAAAAAAAAaAEAAAAAAABwAQAAAAAAAHgBAAAAAAAAgAEAAAAAAACIAQAAAAAAAJABAAAAAAAAmAEAAAAAAACgAQAAAAAAAKgBAAAAAAAAsAEAAAAAAAC4AQAAAAAAAMABAAAAAAAAyAEAAAAAAADQAQAAAAAAANgBAAAAAAAA4AEAAAAAAADoAQAAAAAAAPABAAAAAAAA+AEAAAAAAAAAAgAAAAAAAAgCAAAAAAAAEAIAAAAAAAAYAgAAAAAAACACAAAAAAAAKAIAAAAAAAAwAgAAAAAAADgCAAAAAAAAQAIAAAAAAABIAgAAAAAAAFACAAAAAAAAWAIAAAAAAABgAgAAAAAAAGgCAAAAAAAAcAIAAAAAAAB4AgAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAGAAAAEAAAAAAAAAAAAAAABwAAABAAAAAAAAAAAAAAAAgAAAAQAAAAAAAAAAAAAAAKAAAAEAAAAAAAAAAAAAAACwAAABAAAAAAAAAAAAAAAA0AAAAQAAAAAAAAAAAAAAAOAAAAEAAAAAAAAAABAAAAAAAAABAAAAAAAAAAAQAAAAYAAAAQAAAAAAAAAAEAAAAHAAAAEAAAAAAAAAABAAAACAAAABAAAAAAAAAAAQAAAAoAAAAQAAAAAAAAAAEAAAALAAAAEAAAAAAAAAABAAAADQAAABAAAAAAAAAAAQAAAA4AAAAQAAAAAAAAAAQAAAAAAAAAEAAAAAAAAAAEAAAABgAAABAAAAAAAAAABAAAAAcAAAAQAAAAAAAAAAQAAAAIAAAAEAAAAAAAAAAEAAAACgAAABAAAAAAAAAABAAAAAsAAAAQAAAAAAAAAAQAAAANAAAAEAAAAAAAAAAEAAAADgAAABAAAAAAAAAABwAAAAAAAAAQAAAAAAAAAAcAAAAGAAAAEAAAAAAAAAAHAAAABwAAABAAAAAAAAAABwAAAAgAAAAQAAAAAAAAAAcAAAAKAAAAEAAAAAAAAAAHAAAACwAAABAAAAAAAAAABwAAAA0AAAAQAAAAAAAAAAcAAAAOAAAAEAAAAAAAAAAKAAAAAAAAABAAAAAAAAAACgAAAAYAAAAQAAAAAAAAAAoAAAAHAAAAEAAAAAAAAAAKAAAACAAAABAAAAAAAAAACgAAAAoAAAAQAAAAAAAAAAoAAAALAAAAEAAAAAAAAAAKAAAADQAAABAAAAAAAAAACgAAAA4AAAAIAAAAAAAAAAgAAAAAAAAA --shared-files --seatbelt-client=38
the347           11464   0.0  0.3 450558176 113200   ??  S     1:12PM   5:26.58 /Applications/ATV Remote.app/Contents/MacOS/ATV Remote
the347           11486   0.0  0.2 449989152  64080   ??  S     1:12PM   0:24.55 /Applications/ATV Remote.app/Contents/Frameworks/ATV Remote Helper (Renderer).app/Contents/MacOS/ATV Remote Helper (Renderer) --type=renderer --field-trial-handle=1718379636,488578797432545079,6026224567748839752,131072 --disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies,SpareRendererForSitePerProcess,WindowCaptureMacV2 --lang=en-US --app-path=/Applications/ATV Remote.app/Contents/Resources/app.asar --no-sandbox --no-zygote --num-raster-threads=4 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --renderer-client-id=5 --no-v8-untrusted-code-mitigations --shared-files
the347           11483   0.0  0.4 450364528 148416   ??  S     1:12PM   5:04.16 /Applications/ATV Remote.app/Contents/Frameworks/ATV Remote Helper (Renderer).app/Contents/MacOS/ATV Remote Helper (Renderer) --type=renderer --field-trial-handle=1718379636,488578797432545079,6026224567748839752,131072 --disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies,SpareRendererForSitePerProcess,WindowCaptureMacV2 --lang=en-US --app-path=/Applications/ATV Remote.app/Contents/Resources/app.asar --no-sandbox --no-zygote --num-raster-threads=4 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --renderer-client-id=4 --no-v8-untrusted-code-mitigations --shared-files
the347           11482   0.0  0.0 411177328  15984   ??  S     1:12PM   0:00.11 /Applications/ATV Remote.app/Contents/Frameworks/ATV Remote Helper.app/Contents/MacOS/ATV Remote Helper --type=utility --utility-sub-type=network.mojom.NetworkService --field-trial-handle=1718379636,488578797432545079,6026224567748839752,131072 --disable-features=CookiesWithoutSameSiteMustBeSecure,SameSiteByDefaultCookies,SpareRendererForSitePerProcess,WindowCaptureMacV2 --lang=en-US --service-sandbox-type=network --shared-files --seatbelt-client=47

Further investigation

By manually running start_server.sh, we can see a crash-loop on AttributeError: 'bool' object has no attribute 'keyboard'

$ ~/Library/Application\ Support/ATV\ Remote/start_server.sh
...
 connection is CONNECTING
< GET / HTTP/1.1
< Sec-WebSocket-Version: 13
< Sec-WebSocket-Key: nFleKlZ49DUOFzT08KrZ7g==
< Connection: Upgrade
< Upgrade: websocket
< Host: localhost:8765
> HTTP/1.1 101 Switching Protocols
> Upgrade: websocket
> Connection: Upgrade
> Sec-WebSocket-Accept: /qNpriUUokH9UrwsQdG4bsjDuiE=
> Date: Sat, 09 Nov 2024 17:41:44 GMT
> Server: Python/3.9 websockets/13.1
connection open
= connection is OPEN
< TEXT '{"cmd":"kbfocus","data":""}' [27 bytes]
connection handler failed
Traceback (most recent call last):
  File "/Users/the347/Library/Application Support/ATV Remote/env/lib/python3.9/site-packages/websockets/legacy/server.py", line 245, in handler
    await self.ws_handler(self)
  File "/Users/the347/Library/Application Support/ATV Remote/wsserver.py", line 259, in ws_main
    await parseRequest(j, websocket)
  File "/Users/the347/Library/Application Support/ATV Remote/wsserver.py", line 144, in parseRequest
    kbfocus = active_device.keyboard.text_focus_state == pyatv.const.KeyboardFocusState.Focused
AttributeError: 'bool' object has no attribute 'keyboard'
! failing connection with code 1011
= connection is CLOSING
> CLOSE 1011 (internal error) [2 bytes]
connection closed
...
[repeats until killed]
bsharper commented 4 days ago

Have you tried deleting ~/Library/Application Support/ATV Remote?

It seems like an older version of pyatv is running on your system. The error shown above points to something that definitely exists in the existing / current version of pyatv (which is the primary library this application uses for talking to the ATV). If you delete the ATV Remote folder, the latest version of pyatv should be installed and updated.

jklewa commented 3 days ago

@bsharper I have not tried deleting that directory, but the pyatv version doesn't seem to be the issue here.

The AttributeError is on the other side of the expression

kbfocus = active_device.keyboard.text_focus_state == pyatv.const.KeyboardFocusState.Focused AttributeError: 'bool' object has no attribute 'keyboard'

bsharper commented 3 days ago

@jklewa Good observation. Both sides of the comparison are from pyatv, active_device is whatever pyatv.interface.AppleTV instance is returned from pyatv.connect, and the other is just a const for the keyboard focus.

Also are you using v1.1.1 or v1.1.3? I think there are some minor fixes in v1.1.3 related to startup sequencing where the app would ask if the keyboard was open without being paired properly. The fact that the error you shared was seeing the initial boolean value for active_device this might be what you're seeing, and using the updated version and re-pairing should fix it.

It's possible only the Airplay protocol is connecting, and the Companion protocol isn't. If you pair with your Apple TV again, you should see 2 pairing screens. Text input is handled by the Companion protocol (I believe), so it's possible if it's only using the Airplay protocol it might not be connecting properly.

You can also try clearing out the settings by opening the app then pressing Cmd+option+i to open Development Tools. Then go to the Console and type localStorage.clear() by the > in the bottom and press enter. Then press cmd+R to refresh the app and close the Dev Tools window. You should see a "first time" popup if this worked correctly. Then pair again and see if the issue is still happening.

Good luck and let me know if it works for you.

jklewa commented 2 days ago

@bsharper ~At the time, I was running v1.1.1.~ The stack trace above references code introduced in v1.1.3, so I must have been running v1.1.3 at the time.

Since then, I've been running a fork of main and haven't experienced the bug again.