Skip to content

Dangerous suppressions of exceptions when closing or terminating PyAudioSourceReader #4

Description

@charlie-dufort

The following suppressions of exceptions in audio.BasePyAudioSourceReader can lead to bugs when reinstanciating or reopening PyAudioSourceReader, because the PortAudio resources have not been released:

    def close(self):
        """Stop audio stream"""
        with suppress(Exception):
            self._fp.stop_stream()
        with suppress(Exception):
            self._fp.close()
        self._terminate_pyaudio()

     @classmethod
     def _terminate_pyaudio(cls):
        with suppress(Exception):
            if cls._pyaudio_instance is not None and isinstance(
                cls._pyaudio_instance, (pyaudio.PyAudio,)
            ):
                cls._pyaudio_instance.terminate()
                cls._pyaudio_instance = None

An alternative could be to try to close / terminate PyAudioSourceReader several times in a loop, until success or a maximum number of attempts has been reached, in which case an exception would be raised for good.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions