spatialaudio / python-sounddevice

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

import sounddevice blocks the application (and can't be killed) #435

Open LOGUNIVPM opened 1 year ago

LOGUNIVPM commented 1 year ago

Hi all, I've been using a small GUI application written in python 3 and kivy that uses sounddevice to record and analyze audio.

This works well on my Linux mint 19 machine. Now I am launching the application on a fresh Ubuntu 20 install on a NUC and I have the following problem: on application launch I see a black GUI, nothing happens, the application can't be killed (not even with kill -9 from command line). I have started debugging the application from PyCharm and it seems that the program is locked on import sounddevice. After that line the debugger loses control and can't even kill the application. Maybe sounddevice got the wrong audio card and gets lost? I don't know...

I've got Ubuntu 20.04.4 on a NUC 9i9 with: Card: sof-hda-dsp Chip: Realtek ALC256 and a graphic card: product: TU117 [GeForce GTX 1650] [10DE:1F82] which is also seen as audio device (through HDMI) and also Chip: Nvidia GPU 94 HDMI/DP

Currently I have the nuveau drivers installed but the official nvidia drivers behave the same. What can be wrong?!

LOGUNIVPM commented 1 year ago

Hi, hope someone can give me some hints to debug the problem.

The problem has a minimum working example that is simple as that: open a terminal and call python3, then in the python commandline just type: import sounddevice

The python commandline will NEVER return, so you are stuck.

I also removed the video card, just in case the hdmi out create problems, but nothing improved.

LOGUNIVPM commented 1 year ago

More info:

[ 71.162615] BUG: kernel NULL pointer dereference, address: 0000000000000000 [ 71.162628] #PF: supervisor instruction fetch in kernel mode [ 71.162633] #PF: error_code(0x0010) - not-present page [ 71.162638] PGD 16a91b067 P4D 16a91b067 PUD 16a91c067 PMD 0 [ 71.162649] Oops: 0010 [#1] SMP NOPTI [ 71.162657] CPU: 11 PID: 2622 Comm: python3 Not tainted 5.15.0-50-generic #56~20.04.1-Ubuntu [ 71.162666] Hardware name: Intel(R) Client Systems NUC9i9QNX/NUC9i9QNB, BIOS QXCFL579.0054.2020.0810.2239 08/10/2020 [ 71.162670] RIP: 0010:0x0 [ 71.162683] Code: Unable to access opcode bytes at RIP 0xffffffffffffffd6. [ 71.162687] RSP: 0018:ffffb77bc1d7f820 EFLAGS: 00010202 [ 71.162693] RAX: 0000000000000000 RBX: ffff9ce7d3a40600 RCX: 0000000000000007 [ 71.162699] RDX: ffff9ce7c7b77800 RSI: ffff9ce7c6f78028 RDI: ffff9ce7d3a40660 [ 71.162703] RBP: ffffb77bc1d7f848 R08: 000000000000006c R09: ffff9ce7d81723c7 [ 71.162708] R10: 0000000000000007 R11: ffffffffc0b1159c R12: ffff9ce7c7b77800 [ 71.162712] R13: 0000000000000001 R14: ffff9ce7c6f78028 R15: ffff9ce7d1bfc128 [ 71.162716] FS: 00007f17a10ce740(0000) GS:ffff9cef60ec0000(0000) knlGS:0000000000000000 [ 71.162723] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 71.162728] CR2: ffffffffffffffd6 CR3: 0000000117db6005 CR4: 00000000003706e0 [ 71.162733] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 71.162737] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 71.162741] Call Trace: [ 71.162745] [ 71.162752] hdac_hda_dai_open+0x86/0xc6 [snd_soc_hdac_hda] [ 71.162766] snd_soc_dai_startup+0x34/0x70 [snd_soc_core] [ 71.162817] ? snd_soc_component_module_get+0x4d/0x60 [snd_soc_core] [ 71.162865] soc_pcm_open+0x13d/0x4d0 [snd_soc_core] [ 71.162912] dpcm_be_dai_startup+0x1d3/0x590 [snd_soc_core] [ 71.162958] dpcm_fe_dai_startup+0x89/0xa00 [snd_soc_core] [ 71.163005] dpcm_fe_dai_open+0x95/0x260 [snd_soc_core] [ 71.163052] snd_pcm_open_substream+0x95/0x330 [snd_pcm] [ 71.163075] snd_pcm_open+0xf2/0x230 [snd_pcm] [ 71.163095] ? wake_up_q+0x90/0x90 [ 71.163106] snd_pcm_capture_open+0x45/0x70 [snd_pcm] [ 71.163126] snd_open+0xb9/0x1b0 [snd] [ 71.163143] chrdev_open+0xd0/0x1c0 [ 71.163154] ? cdev_default_release+0x30/0x30 [ 71.163164] do_dentry_open+0x157/0x390 [ 71.163174] vfs_open+0x2d/0x40 [ 71.163183] do_open.isra.0+0x20d/0x480 [ 71.163192] path_openat+0x18e/0xe50 [ 71.163202] do_filp_open+0xb2/0x120 [ 71.163211] ? __check_object_size+0x14f/0x160 [ 71.163220] ? alloc_fd+0x58/0x190 [ 71.163231] do_sys_openat2+0x249/0x330 [ 71.163241] do_sys_open+0x46/0x80 [ 71.163250] x64_sys_openat+0x20/0x30 [ 71.163260] do_syscall_64+0x59/0xc0 [ 71.163268] ? syscall_exit_to_user_mode+0x27/0x50 [ 71.163278] ? do_syscall_64+0x69/0xc0 [ 71.163285] ? syscall_exit_to_user_mode+0x27/0x50 [ 71.163293] ? x64_sys_close+0x12/0x50 [ 71.163301] ? do_syscall_64+0x69/0xc0 [ 71.163308] entry_SYSCALL_64_after_hwframe+0x61/0xcb [ 71.163319] RIP: 0033:0x7f17a13a7d3b [ 71.163325] Code: 25 00 00 41 00 3d 00 00 41 00 74 4b 64 8b 04 25 18 00 00 00 85 c0 75 67 44 89 e2 48 89 ee bf 9c ff ff ff b8 01 01 00 00 0f 05 <48> 3d 00 f0 ff ff 0f 87 91 00 00 00 48 8b 4c 24 28 64 48 33 0c 25 [ 71.163333] RSP: 002b:00007ffc088c99f0 EFLAGS: 00000246 ORIG_RAX: 0000000000000101 [ 71.163340] RAX: ffffffffffffffda RBX: 0000000000080802 RCX: 00007f17a13a7d3b [ 71.163345] RDX: 0000000000080802 RSI: 00007ffc088c9bb0 RDI: 00000000ffffff9c [ 71.163349] RBP: 00007ffc088c9bb0 R08: 0000000000000000 R09: 0000000000000011 [ 71.163353] R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000080802 [ 71.163358] R13: 0000000000000004 R14: 0000000000000000 R15: 00007ffc088c9bb0 [ 71.163364] [ 71.163367] Modules linked in: rfcomm cmac algif_hash algif_skcipher af_alg bnep binfmt_misc nls_iso8859_1 snd_soc_skl_hda_dsp snd_soc_intel_hda_dsp_common snd_soc_hdac_hdmi snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic snd_soc_dmic snd_sof_pci_intel_cnl snd_sof_intel_hda_common soundwire_intel soundwire_generic_allocation soundwire_cadence snd_sof_intel_hda snd_sof_pci snd_sof_xtensa_dsp snd_sof snd_soc_hdac_hda snd_hda_ext_core snd_soc_acpi_intel_match snd_soc_acpi soundwire_bus ledtrig_audio snd_soc_core snd_compress ac97_bus snd_pcm_dmaengine intel_rapl_msr intel_rapl_common intel_tcc_cooling snd_hda_intel x86_pkg_temp_thermal snd_intel_dspcfg intel_powerclamp snd_intel_sdw_acpi snd_usb_audio snd_hda_codec coretemp snd_usbmidi_lib snd_hda_core mc snd_hwdep kvm_intel mei_hdcp snd_pcm i915 kvm iwlmvm crct10dif_pclmul ghash_clmulni_intel snd_seq_midi snd_seq_midi_event mac80211 aesni_intel snd_rawmidi crypto_simd cryptd libarc4 ttm snd_seq rapl snd_seq_device iwlwifi [ 71.163481] drm_kms_helper intel_cstate btusb btrtl snd_timer cec btbcm btintel joydev rc_core input_leds bluetooth intel_wmi_thunderbolt snd mei_me cfg80211 fb_sys_fops ucsi_acpi syscopyarea ecdh_generic sysfillrect soundcore 8250_dw ee1004 ecc mei serio_raw typec_ucsi wmi_bmof sysimgblt typec intel_pch_thermal acpi_pad mac_hid acpi_tad sch_fq_codel ipmi_devintf ipmi_msghandler msr parport_pc ppdev lp parport ramoops drm reed_solomon pstore_blk pstore_zone efi_pstore ip_tables x_tables autofs4 hid_generic usbhid hid nvme crc32_pclmul igb psmouse nvme_core i2c_algo_bit i2c_i801 dca e1000e thunderbolt i2c_smbus intel_lpss_pci ahci intel_lpss xhci_pci libahci xhci_pci_renesas idma64 wmi video pinctrl_cannonlake [ 71.163603] CR2: 0000000000000000 [ 71.163608] ---[ end trace 072c2631fd8c8450 ]--- [ 72.120150] RIP: 0010:0x0 [ 72.120153] Code: Unable to access opcode bytes at RIP 0xffffffffffffffd6. [ 72.120155] RSP: 0018:ffffb77bc1d7f820 EFLAGS: 00010202 [ 72.120157] RAX: 0000000000000000 RBX: ffff9ce7d3a40600 RCX: 0000000000000007 [ 72.120159] RDX: ffff9ce7c7b77800 RSI: ffff9ce7c6f78028 RDI: ffff9ce7d3a40660 [ 72.120160] RBP: ffffb77bc1d7f848 R08: 000000000000006c R09: ffff9ce7d81723c7 [ 72.120162] R10: 0000000000000007 R11: ffffffffc0b1159c R12: ffff9ce7c7b77800 [ 72.120163] R13: 0000000000000001 R14: ffff9ce7c6f78028 R15: ffff9ce7d1bfc128 [ 72.120164] FS: 00007f17a10ce740(0000) GS:ffff9cef60ec0000(0000) knlGS:0000000000000000 [ 72.120166] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 72.120167] CR2: ffffffffffffffd6 CR3: 0000000117db6005 CR4: 00000000003706e0 [ 72.120169] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 72.120170] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400

mgeier commented 1 year ago

Wow, that sounds mysterious!

I don't really have a clue what could go wrong there, but there is one thing you could try: you could disable the call to _initialize():

https://github.com/spatialaudio/python-sounddevice/blob/841e0a1b2ea2367476248e5f0fc6878154949417/sounddevice.py#L2878

This should at least let you import sounddevice as sd.

After that, you can manually call sd._lib.Pa_Initialize() and see if that works.

If that works, the problem has likely to do with the redirection of stderr output, which is done in _initialize().

If not, I guess that's a problem with the underlying PortAudio library.

This issue may or may not be related: #90.

LOGUNIVPM commented 1 year ago

Hi, I only had now the chance to try it.

Commenting the Initialize() solves the crash. This is what happens when I later do the Initialize manually:

Python 3.9.14 (main, Sep  7 2022, 23:43:29) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sounddevice as sd
>>> sd._lib.Pa_Initialize()
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefault
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM dmix
0
mgeier commented 1 year ago

Thanks for the update!

According to your output, Pa_Initialize() is successful.

The many error messages are expected, and are the reason why I'm trying to redirect stderr.

So the problem indeed seems to be caused by the redirection. Can you please try the minimal reproducing example from https://github.com/spatialaudio/python-sounddevice/issues/90#issuecomment-1182709958?

mgeier commented 1 year ago

Can you please try if #444 works?

LOGUNIVPM commented 1 year ago

Yes, the fix works! I followed your commits in #444 I assume that the problem is already fixed if instead of installing with pip, one clones sounddevice from the github repo, right?

mgeier commented 1 year ago

Thanks for checking this!

I assume that the problem is already fixed if instead of installing with pip, one clones sounddevice from the github repo, right?

No, it's not yet merged into this repo, because it hasn't been tested on Windows yet. But you should be able to install it with something like:

python3 -m pip install git+https://github.com/mgeier/python-sounddevice@redirect-with-dup2
LOGUNIVPM commented 1 year ago

Uhm, is the branch redirect-with-dup2 correct? I installed that but still get the kernel fault.

LOGUNIVPM commented 1 year ago

Here's a bit more detail with respect to my previous comment. On the same machine where I had previously fixed the issue with success I reinstalled Ubuntu, this time the 2022 LTS instead of the 2020 one. I installed the redirect-with-dup2 branch for sounddevice and I stumbled in the same issue. I have reviewed the code, made some changes and I see that the kernel fault is triggered by Pa_initialize: _check(_lib.Pa_Initialize(), 'Error initializing PortAudio')

I will revert to Ubuntu 2020 LTS and see if things get solved...

mgeier commented 1 year ago

Thanks for the update!

If Pa_Initialize() makes problems, there isn't much I can do on the Python side, this would have to be reported to the PortAudio project.

But it seems that os.dup() and os.dup2() are working, that's good to know.