Skip to content
New issue

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

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

Already on GitHub? # to your account

Hard crash after sound finishes playing with python 3.12 #72

Open
scottnm opened this issue Jan 16, 2024 · 5 comments · Fixed by wasertech/py-simple-audio#1
Open

Hard crash after sound finishes playing with python 3.12 #72

scottnm opened this issue Jan 16, 2024 · 5 comments · Fixed by wasertech/py-simple-audio#1

Comments

@scottnm
Copy link

scottnm commented Jan 16, 2024

overview

simpleaudio 1.0.4 seems to crash for me when using python 3.12

the same crash does not reproduce in python 3.11

the crash does not appear to be any form of python failure that I can either catch as an exception or intercept with a python debugger

example

Here's the sample code I'm testing with

try:
    wav_file = simpleaudio.WaveObject.from_wave_file('test.wav')
    print('playing sound(s) using simpleaudio')
    play_obj = wav_file.play()
    play_obj.wait_done()
    print('done')
    play_obj.stop()
    print('stopped')
except BaseException as ex:
    print(f"caught exception {ex}")

when run from a python 3.12 environment with simpleaudio 1.0.4, this is the output

> python .\main.py
playing sound(s) using simpleaudio

here's the same run but from a python 3.11 environment

> python .\main.py
playing sound(s) using simpleaudio
done
stopped

Repro Environment information

  • full python versions: 3.11.0 and 3.12.1
  • OS: Windows 10
@scottnm
Copy link
Author

scottnm commented Jan 16, 2024

for additional context, I found another user on reddit who seemed to be experiencing a similar problem (although their repro was via pydub) https://www.reddit.com/r/pythonhelp/comments/17vw5zy/python_is_hard_crashing_with_a_simple_mp3_play/

though if I understand correctly, pydub uses simpleaudio for playback

cexen added a commit to cexen/py-simple-audio that referenced this issue Jan 21, 2024
@cexen
Copy link

cexen commented Jan 21, 2024

The cause of the segmentation fault seems to be calling PyMem_Free(*) without acquiring the GIL.

There are two solutions (both of which solved the problem for me). :

  1. Replace all PyMem_Malloc(*) with PyMem_RawMalloc(*) and PyMem_Free(*) with PyMem_RawFree(*).
  2. Sandwich all PyMem_Free(*) between PyGILState_STATE gstate = PyGILState_Ensure(); and PyGILState_Release(gstate);.

The former solution is probably better.

With the above changes and rebuild I am able to use simpleaudio in Py3.12.
https://github.com/cexen/py-simple-audio

pip install -U --force git+https://github.com/cexen/py-simple-audio.git

I am not familiar enough with package development (especially CI and backward compatibility) to know how to make the necessary changes for users in all environments...

@Spacexplorer11
Copy link

@cexen Thanks a lot! I got the same issue and this fixed it!

@andwerb
Copy link

andwerb commented Jul 27, 2024

Yeah this bug renders simpleaudio unusable.

@jamesdh
Copy link

jamesdh commented Dec 17, 2024

Also experiencing this with Python 3.12.8, although for me I get the warning "There appear to be 1 leaked semaphore objects to clean up at shutdown". Installing @cexen's version fixed it for me, otherwise simple audio appears to be dead in the water with Python 3.12+

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

Successfully merging a pull request may close this issue.

5 participants