Skip to content

Python stream is already registered #292

Closed
@Derkades

Description

@Derkades

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions