Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python stream is already registered #292

Closed
Derkades opened this issue Dec 13, 2024 · 2 comments
Closed

Python stream is already registered #292

Derkades opened this issue Dec 13, 2024 · 2 comments

Comments

@Derkades
Copy link

Derkades commented Dec 13, 2024

hi, thanks for this library, it is very useful.

occasionally I am running into this error when using play_bytes():

Traceback (most recent call last):
  File "/home/robin/.local/lib/python3.13/site-packages/aiohttp/web_protocol.py", line 480, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robin/.local/lib/python3.13/site-packages/aiohttp/web_app.py", line 569, in _handle
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robin/git/raphson/music-headless/raphson_music_headless/server.py", line 113, in _next
    await self.player.next(retry=False)
  File "/home/robin/git/raphson/music-headless/raphson_music_headless/player/mpv_player.py", line 80, in next
    self.player.play_bytes(download.audio)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/robin/.local/lib/python3.13/site-packages/mpv.py", line 2003, in play_bytes
    @self.python_stream(stream_name)
     ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/home/robin/.local/lib/python3.13/site-packages/mpv.py", line 1950, in register
    raise KeyError('Python stream name "{}" is already registered'.format(name))
KeyError: 'Python stream name "__python_mpv_play_generator_8787028885788" is already registered'

I saw in the source code that the stream name is generated like this:

frame = sys._getframe()
stream_name = f'__python_mpv_play_generator_{hash(frame)}'

I don't know what a frame is really, but apparently it is not unique for every call. I replaced it by this:

stream_name = f'__python_mpv_play_generator_{hash(data)}'

and that seemed to work. Is there any reason it doesn't use a randomly generated identifier? Like uuid.uuid4() or secrets.token_urlsafe(), for example

@jaseg
Copy link
Owner

jaseg commented Dec 21, 2024

Oh, lol. That's a good catch. I've fixed this on main and the fix will land in the next minor version.

The fix is that the underlying python_stream API now just defaults to using the identity of the callback function

@jaseg jaseg closed this as completed Dec 21, 2024
jaseg pushed a commit that referenced this issue Dec 21, 2024
…n streams

Frame hashes are not unique since the frame isn't kept around for the
life time of the stream.

Fixes #292.
@Derkades
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants