From 51b3864ae0ddfee24a0140407b2b28d7d7321729 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 7 Feb 2025 23:02:19 +1100 Subject: [PATCH] Fix #131947: Error closing prefs while installing extensions on WIN32 Using `signal.SIGINT` to send a signal to a sub-process isn't supported on WIN32, replace with `signal.CTRL_BREAK_EVENT`. --- scripts/addons_core/bl_pkg/bl_extension_utils.py | 14 ++++++++++++-- scripts/addons_core/bl_pkg/cli/blender_ext.py | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/scripts/addons_core/bl_pkg/bl_extension_utils.py b/scripts/addons_core/bl_pkg/bl_extension_utils.py index fe7efe9290b..57d7f68284b 100644 --- a/scripts/addons_core/bl_pkg/bl_extension_utils.py +++ b/scripts/addons_core/bl_pkg/bl_extension_utils.py @@ -232,7 +232,13 @@ def command_output_from_json_0( # Note that the context-manager isn't used to wait until the process is finished as # the function only finishes when `poll()` is not none, it's just use to ensure file-handles # are closed before this function exits, this only seems to be a problem on WIN32. - with subprocess.Popen(cmd, stdout=subprocess.PIPE) as ps: + + # WIN32 needs to use a separate process-group else Blender will recieve the "break", see #131947. + creationflags = 0 + if sys.platform == "win32": + creationflags = subprocess.CREATE_NEW_PROCESS_GROUP + + with subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=creationflags) as ps: stdout = ps.stdout assert stdout is not None @@ -291,7 +297,11 @@ def command_output_from_json_0( # It also means a request to exit might not be responded to soon enough. request_exit = yield json_messages if request_exit and not request_exit_signal_sent: - ps.send_signal(signal.SIGINT) + if sys.platform == "win32": + # Caught by the `signal.SIGBREAK` signal handler. + ps.send_signal(signal.CTRL_BREAK_EVENT) + else: + ps.send_signal(signal.SIGINT) request_exit_signal_sent = True diff --git a/scripts/addons_core/bl_pkg/cli/blender_ext.py b/scripts/addons_core/bl_pkg/cli/blender_ext.py index c6852f85b32..6af19027605 100755 --- a/scripts/addons_core/bl_pkg/cli/blender_ext.py +++ b/scripts/addons_core/bl_pkg/cli/blender_ext.py @@ -5579,6 +5579,9 @@ def main( # Run early to prevent a `KeyboardInterrupt` exception. signal.signal(signal.SIGINT, signal_handler_sigint) + if sys.platform == "win32": + # WIN32 needs to check for break as sending SIGINT isn't supported from the caller, see #131947. + signal.signal(signal.SIGBREAK, signal_handler_sigint) # Needed on WIN32 which doesn't default to `utf-8`. for fh in (sys.stdout, sys.stderr):