aliencaocao / netease_cloudmusic_discord_rpc

网易云音乐Discord听歌状态同步
GNU Affero General Public License v3.0
53 stars 5 forks source link

Update fails after Discord restart #18

Closed FrzMtrsprt closed 1 year ago

FrzMtrsprt commented 1 year ago

After restarting Discord, update will fail with an InvalidID exception. The traceback is as follows.

  File "...\pypresence\baseclient.py", line 81, in read_output
    preamble = await self.sock_reader.read(8)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [WinError 232] The pipe has been ended.

During handling of the above exception, another exception occurred:

  File "...\pypresence\baseclient.py", line 85, in read_output
    raise InvalidID
  File "...\pypresence\presence.py", line 34, in update
    return self.loop.run_until_complete(self.read_output())
  File "...\main.py", line 227, in update
    RPC.update(pid=pid,
  File "...\main.py", line 47, in _target
    self.function()
pypresence.exceptions.InvalidID: Client ID is Invalid

This is clearly pypresence's bug (see #213), and we'll have to wait for pypresence to fix this.

Below is a temporary fix.

try:
    RPC.update(state="test")  # InvalidID after Discord restart
except InvalidID:
    print("Reconnecting")
    RPC.connect()  # RuntimeError: There is no current event loop in thread 'Thread-1 (_target)'.

Then we run into the second bug: RuntimeError: There is no current event loop in thread 'Thread-1 (_target)'.

It happened when Presence tries to renew the event loop in connect(), but fails to handle the exception when it can't find a existing one.

This bug is fixed in #210, which has been waiting for approval for two weeks.

aliencaocao commented 1 year ago

Try https://github.com/aliencaocao/netease_cloudmusic_discord_rpc/commit/feb269d866bf4c1e4b56a5ca82ea190ab7feda10

FrzMtrsprt commented 1 year ago

I think it works for now.

aliencaocao commented 1 year ago

Great, then we can forget about upstream bugs

TheSpookyCat commented 1 year ago

You might be better off catching our base exception PyPresenceException to account for the current (and wrong) InvalidID being replaced with PipeClosed. There is no ETA on when the commit including PipeClosed will be pushed to pypi.

https://github.com/qwertyquerty/pypresence/blob/master/pypresence/exceptions.py