spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
980 stars 145 forks source link

Crash/exception on macOS Ventura when numpy not explicitly imported #487

Closed ideoforms closed 10 months ago

ideoforms commented 10 months ago

Long-time user of sounddevice, somewhat baffled by an issue I have hit today.

I recently upgraded my M1 MacBook Pro to macOS Ventura, and have found that even simple scripts that use sounddevice are failing to run, including many long-standing scripts that I have previously run without issues. Here's a MWE:

import time
import sounddevice as sd

def audio_callback(out_data, frames, time, status):
    out_data[:] = 0

with sd.OutputStream(callback=audio_callback):
    while True:
        time.sleep(0.1)

When I run the above in arm64 architecture, the script crashes with a SIGBUS:

Bus error: 10

If I run the same script using intel64, I get:

Exception ignored from cffi callback <function _StreamBase.__init__.<locals>.callback_ptr at 0x10c8fbb00>:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sounddevice.py", line 859, in callback_ptr
    data = _array(
           ^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sounddevice.py", line 2702, in _array
    import numpy as np
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/__init__.py", line 388, in <module>
    raise RuntimeError(msg)
RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend.
If you compiled yourself, more information is available at:
https://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries
Otherwise report this to the vendor that provided NumPy.
UserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero.

I tried reinstalling Python (3.9/3.10,3.11), reinstalling latest sounddevice (0.4.6) and numpy (1.25.2) via pip, but the issue persists.

I've also reproduced this on two other laptops here running Ventura, one M1 and one x86, both using pip to install. I don't have any earlier macOS versions or non-Apple laptops to test on right now.

Resolution / workaround

Finally, I tried adding an explicit import numpy to the top of the script, and it resolves the issue. I'm not clear what is happening here, or why this has only recently started occurring - this explicit import certainly wasn't needed previously.

Looking at sounddevice.py, I can see that it should import numpy on demand when needed, but it doesn't seem to successfully do so.

Any advice welcomed!

mgeier commented 10 months ago

Thanks for reporting this, this is an interesting and quite strange issue!

I don't know what's the underlying cause, but it's great that you found out that import numpy resolves the issue.

Looking at sounddevice.py, I can see that it should import numpy on demand when needed, but it doesn't seem to successfully do so.

Yeah, looks like it.

In #488, I'm trying to call import numpy earlier, can you please try if that also works for you (without using import numpy in your script)?

ideoforms commented 10 months ago

@mgeier Yes, that fixes it! Thank you!

Very curious one. I guess maybe it's something to do with importing numpy from a cffi callback? I know I've had a few issues on recent Macs with CFFI and memory protection, but I've never really dived deep enough to properly understand it. 🤷🏻‍♂️

mgeier commented 10 months ago

I guess it is related to importing it from the callback, but I don't know whether it's because of how CFFI does things, or because it is called from a a thread that's not created by Python, or maybe something entirely different.

PixelsTv commented 2 months ago

Long-time user of sounddevice, somewhat baffled by an issue I have hit today.

I recently upgraded my M1 MacBook Pro to macOS Ventura, and have found that even simple scripts that use sounddevice are failing to run, including many long-standing scripts that I have previously run without issues. Here's a MWE:

import time
import sounddevice as sd

def audio_callback(out_data, frames, time, status):
    out_data[:] = 0

with sd.OutputStream(callback=audio_callback):
    while True:
        time.sleep(0.1)

When I run the above in arm64 architecture, the script crashes with a SIGBUS:

Bus error: 10

If I run the same script using intel64, I get:

Exception ignored from cffi callback <function _StreamBase.__init__.<locals>.callback_ptr at 0x10c8fbb00>:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sounddevice.py", line 859, in callback_ptr
    data = _array(
           ^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/sounddevice.py", line 2702, in _array
    import numpy as np
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/__init__.py", line 388, in <module>
    raise RuntimeError(msg)
RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend.
If you compiled yourself, more information is available at:
https://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries
Otherwise report this to the vendor that provided NumPy.
UserWarning: The value of the smallest subnormal for <class 'numpy.float64'> type is zero.

I tried reinstalling Python (3.9/3.10,3.11), reinstalling latest sounddevice (0.4.6) and numpy (1.25.2) via pip, but the issue persists.

I've also reproduced this on two other laptops here running Ventura, one M1 and one x86, both using pip to install. I don't have any earlier macOS versions or non-Apple laptops to test on right now.

Resolution / workaround

Finally, I tried adding an explicit import numpy to the top of the script, and it resolves the issue. I'm not clear what is happening here, or why this has only recently started occurring - this explicit import certainly wasn't needed previously.

Looking at sounddevice.py, I can see that it should import numpy on demand when needed, but it doesn't seem to successfully do so.

Any advice welcomed!

Importing lumpy fixed it thank you so much been looking all over the internet for this