Terminate madmax/bladebit on Windows on Ctrl-C (#9113)

This commit is contained in:
Jeff 2021-11-04 08:59:13 -07:00 committed by GitHub
parent c1796c3b63
commit c80208cda9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
import asyncio
import signal
import subprocess
import sys
# https://kevinmccarthy.org/2016/07/25/streaming-subprocess-stdin-and-stdout-with-asyncio-in-python/
@ -21,10 +23,21 @@ def parse_stdout(out, progress):
async def run_plotter(args, progress_dict):
orig_sigint_handler = signal.getsignal(signal.SIGINT)
installed_sigint_handler = False
process = await asyncio.create_subprocess_exec(
*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
def sigint_handler(signum, frame):
process.terminate()
# For Windows, we'll install a SIGINT handler to catch Ctrl-C (KeyboardInterrupt isn't raised)
if sys.platform in ["win32", "cygwin"]:
signal.signal(signal.SIGINT, sigint_handler)
installed_sigint_handler = True
try:
await asyncio.wait(
[
_read_stream(
@ -39,6 +52,12 @@ async def run_plotter(args, progress_dict):
)
await process.wait()
except Exception as e:
print(f"Caught exception while invoking plotter: {e}")
finally:
# Restore the original SIGINT handler
if installed_sigint_handler:
signal.signal(signal.SIGINT, orig_sigint_handler)
def run_command(args, exc_description, *, check=True, **kwargs) -> subprocess.CompletedProcess: