3
0
Fork 0
mirror of https://github.com/YosysHQ/sby.git synced 2025-04-23 05:15:30 +00:00

Add Windows workaround for forceful subprocess termination.

Signed-off-by: William D. Jones <thor0505@comcast.net>
This commit is contained in:
William D. Jones 2019-03-30 08:13:37 -04:00 committed by Ed Bordin
parent 826cd1a6f2
commit b6007aa68c

View file

@ -114,14 +114,22 @@ class SbyTask:
if self.running:
if not self.silent:
self.job.log("{}: terminating process".format(self.info))
if os.name == "posix":
if os.name != "posix":
# self.p.terminate does not actually terminate underlying
# processes on Windows, so use taskkill to kill the shell
# and children. This for some reason does not cause the
# associated future (self.fut) to complete until it is awaited
# on one last time.
subprocess.Popen("taskkill /T /F /PID {}".format(self.p.pid), stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
try:
os.killpg(self.p.pid, signal.SIGTERM)
except PermissionError:
pass
self.p.terminate()
self.job.tasks_running.remove(self)
all_tasks_running.remove(self)
self.job.tasks_retired.append(self)
self.terminated = True
async def output_async(self):
@ -159,6 +167,7 @@ class SbyTask:
async def shutdown_and_notify(self):
self.job.log("%s: finished (returncode=%d)" % (self.info, self.p.returncode))
self.job.tasks_running.remove(self)
self.job.tasks_retired.append(self)
self.running = False
self.handle_exit(self.p.returncode)
@ -207,6 +216,7 @@ class SbyJob:
self.tasks_running = []
self.tasks_pending = []
self.tasks_retired = []
self.start_clock_time = time()
@ -272,6 +282,14 @@ class SbyJob:
if self.opt_timeout is not None:
timer_fut.cancel()
# Required on Windows. I am unsure why, but subprocesses that were
# terminated will not have their futures complete until awaited on
# one last time.
if os.name != "posix":
for t in self.tasks_retired:
if not t.fut.done():
await t.fut
def log(self, logmessage):
tm = localtime()
print("SBY {:2d}:{:02d}:{:02d} [{}] {}".format(tm.tm_hour, tm.tm_min, tm.tm_sec, self.workdir, logmessage), flush=True)