Marti2203 / test

MIT License
0 stars 0 forks source link

VoiceClient.play() doesn't pass FFMPEG error to 'after' function. #2

Open Marti2203 opened 2 days ago

Marti2203 commented 2 days ago

Summary

VoiceClient.play() doesn't pass FFMPEG error to 'after' function.

Reproduction Steps

I know from https://github.com/Rapptz/discord.py/issues/5131 that the session invalidation error is not a Discord.py error, however, the VoiceClient's play() function does not seem to be capturing and passing the FFMPEG error into my provided after function. I would like to notify when this error happens, so capturing this error would be very helpful. It also appears that Discord.py wrongly states that FFMPEG exited successfully, even when it exits with code 1, which by definition, is not successful.

Here is my current code and log results: Code:

def sync_playback_error(error: Exception = None):
    async def playback_error(error: Exception = None):
        has_error: bool = error != None
        if has_error:
            print('Sending error to chat...')
            await ctx.message.channel.send('A playback error has caused the audio stream to stop.')
        else:
            print('No error in playback!')
    print('Starting async callback...')
    asyncio.run_coroutine_threadsafe(playback_error(error), self.bot.loop)

# Attempt to play ffmpeg source
vc.play(ffmpeg_src, after=sync_playback_error)```
Logs: 

[tls @ 0x5632a384eb80] Error in the pull function. [matroska,webm @ 0x5632a384a600] Read error [tls @ 0x5632a384eb80] The specified session has been invalidated for some reason. Last message repeated 1 times Starting async callback... DEBUG:asyncio:Using selector: EpollSelector No error in playback! 2024-06-15 16:58:28 INFO discord.player ffmpeg process 2801802 successfully terminated with return code of 1.


### Minimal Reproducible Code

```python
None

Expected Results

I expected the FFMPEG error to be passed to the after function

Actual Results

The error was not captured and passed to the after function

Intents

discord.Intents.default()

System Information

Python v3.10.12-final discord.py v2.3.2-final aiohttp v3.9.5 system info: Linux 5.15.0-112-generic https://github.com/Rapptz/discord.py/pull/122 SMP Thu May 23 07:48:21 UTC 2024

Checklist

Additional Context

No response

Marti2203 commented 2 days ago

@acr-bot

github-actions[bot] commented 2 days ago

Here is a potential patch for reference:

diff --git a/discord/player.py b/discord/player.py
index 5b2c99d..d47183d 100644
--- a/discord/player.py
+++ b/discord/player.py
@@ -765,6 +765,8 @@ class AudioPlayer(threading.Thread):

         if client.is_connected():
             self.send_silence()
+        else:
+            self._current_error = Exception("FFMPEG process exited with an error.")

     def run(self) -> None:
         try:
@@ -775,6 +777,8 @@ class AudioPlayer(threading.Thread):
         finally:
             self._call_after()
             self.source.cleanup()
+            if self._current_error is None and self.source.error:
+                self._current_error = self.source.error

     def _call_after(self) -> None:
         error = self._current_error
diff --git a/discord/voice_client.py b/discord/voice_client.py
index 3e1c6a5..c2409e0 100644
--- a/discord/voice_client.py
+++ b/discord/voice_client.py
@@ -495,6 +495,9 @@ class VoiceClient(VoiceProtocol):

         self._player = AudioPlayer(source, self, after=after)
         self._player.start()
+        if self._player._current_error:
+            if after:
+                after(self._player._current_error)

     def is_playing(self) -> bool:
         """Indicates if we're currently playing audio."""

This run costs 0.11 USD.