From b72bfd0fd0475cd6fe9c3753df83d765418f04e1 Mon Sep 17 00:00:00 2001 From: Nikolas Dahn <41790746+ndahn@users.noreply.github.com> Date: Wed, 22 Apr 2026 23:41:31 +0200 Subject: [PATCH] Fix stderr handling for frozen Windows applications Prevent stderr duplication issues in frozen context on Windows. Fixes #461. --- src/sounddevice.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/sounddevice.py b/src/sounddevice.py index 00fc6f8..97bbe2a 100644 --- a/src/sounddevice.py +++ b/src/sounddevice.py @@ -2925,13 +2925,17 @@ def _initialize(): """ old_stderr = None - try: - old_stderr = _os.dup(2) - devnull = _os.open(_os.devnull, _os.O_WRONLY) - _os.dup2(devnull, 2) - _os.close(devnull) - except OSError: - pass + # Duping std file descriptors in a frozen context on windows will corrupt + # the fd and cause issues with e.g. subprocess.check_output. This happens + # for example in a compiled pyinstaller exe. See #461 for details. + if _sys.platform != 'win32' or not getattr(_sys, 'frozen', False): + try: + old_stderr = _os.dup(2) + devnull = _os.open(_os.devnull, _os.O_WRONLY) + _os.dup2(devnull, 2) + _os.close(devnull) + except OSError: + pass try: _check(_lib.Pa_Initialize(), 'Error initializing PortAudio') global _initialized