Experiment crashes after a certain amount of sound files

I use Win 10, PsychoPy vs 2025.1.1, with headphones. In the Experiment settings I have ptb as sounddevice.

My experiment crashes after a certain, large amount of trials (always the same – roughly 250 ok -, independent of the conditions file content). What seems to cause this is the amount of audio files accumulated by then (?). See the error message below.

My hardware settings:

  • startup plugins: psychopy-sounddevice, psychopy-pyo;
  • audio driver: Primary Sound, ASIO, Audigy

I read in 2 soundfiles per trial (plus multiple images) using variables, defined in a code snippet at the beginning of the trials. The variables refers to columns in a conditions file.

Code snippet:

audio1_path=start_audio
audio2_path=main_audio
audio1=sound.Sound(audio1_path)
audio2=sound.Sound(audio2_path)
audio1._isFinished =False
audio2._isFinished =False

Audio component in Builder:

Sound - audio1_path or audio2_path

Stop - condition: $audio1._isFinished or $audio2._isFinished

I did not find any solution so far - whichever PC, laptop, headphone etc. I use. Others also seems to have issues with sound files recently, according to posts in the forum.

I need urgent help. I have to solve this issue as I have a very limited time window for testing. I am not a programming/python expert.

The error message I get:

Exception ignored in: <function Stream._del_ at 0x0000025CD8A8EF80>

Traceback (most recent call last):

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 236, in _del_

self.close()

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 146, in close

raise err

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 139, in close

PsychPortAudio('Close', self.handle)

AttributeError: ‘Stream’ object has no attribute ‘handle’

Exception ignored in: <function Stream._del_ at 0x0000025CD8A8EF80>

Traceback (most recent call last):

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 236, in _del_

self.close()

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 146, in close

raise err

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 139, in close

PsychPortAudio('Close', self.handle)

AttributeError: ‘Stream’ object has no attribute ‘handle’

Exception ignored in: <function Stream._del_ at 0x0000025CD8A8EF80>

Traceback (most recent call last):

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 236, in _del_

self.close()

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 146, in close

raise err

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 139, in close

PsychPortAudio('Close', self.handle)

AttributeError: ‘Stream’ object has no attribute ‘handle’

Exception ignored in: <function Stream._del_ at 0x0000025CD8A8EF80>

Traceback (most recent call last):

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 236, in _del_

self.close()

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 146, in close

raise err

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 139, in close

PsychPortAudio('Close', self.handle)

AttributeError: ‘Stream’ object has no attribute ‘handle’

Exception ignored in: <function Stream._del_ at 0x0000025CD8A8EF80>

Traceback (most recent call last):

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 236, in _del_

self.close()

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 146, in close

raise err

File “C:\Program Files\PsychoPy\lib\site-packages\psychtoolbox\audio.py”, line 139, in close

PsychPortAudio('Close', self.handle)

AttributeError: ‘Stream’ object has no attribute ‘handle’

Traceback (most recent call last):

File “C:\Users\…\iDT_test_CORR_FR_lastrun.py”, line 12495, in

run(

File “C:\Users\…\ iDT_test_CORR_FR_lastrun.py”, line 10965, in run

audio1=sound.Sound(audio1_path)

File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\sound\backend_ptb.py”, line 128, in _init_

speaker = SpeakerDevice(speaker)

File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\hardware\speaker.py”, line 112, in _init_

self.createStream()

File “C:\Program Files\PsychoPy\lib\site-packages\psychopy\hardware\speaker.py”, line 247, in createStream

raise ConnectionError(

ConnectionError: Failed to setup a PsychToolBox audio stream for device Haut-parleurs (Logi USB Headset) (8.0).

######### Experiment ended with exit code 1 [pid:6808] #########

Maybe I should close the audio files manually or somehow clear the cache after each trial …?

I tried this at the end of the routines to close files but it did not help…:

audio1.stop()

del audio1

gc.collect

It is either not good or the problem is caused by sth else.

Hello @danna

If you think the experiment crashed due to a memory issue, you should try monitoring memory usage. On a Windows 10 system, run the experiment and use the Windows Task Manager to monitor memory usage. If you want to monitor memory usage in Python, take a look here for instructiosn on how to implement this.

If you assign new values to audio1, the reference stays the same (audio1) and only the object (audio-file1, audio-file2 aso.) changes. Thus, there shouldn’t be an increase in memory usage.

Best wishes Jens

Hi, Thank you.

I managed to solve the issue by creating the sounds at the beginning of the blocks (so outside the trial loops)

main_audio_list = trialfile[‘main_audio’].tolist()

audio2 = sound.Sound(value=“A”)

and then just changing the path within the trial loop:

audio2_path=main_audio_list[trialindex]
audio2.setSound(audio2_path)