Kill process trees on timeouts (os.killpg)

This commit is contained in:
Thomas Nagy 2016-09-03 18:13:38 +02:00
parent afe6862525
commit 9f14c9dda0
No known key found for this signature in database
GPG Key ID: 49B4C67C05277AAA
3 changed files with 18 additions and 11 deletions

View File

@ -334,9 +334,8 @@ class Context(ctx):
cargs = {}
if 'timeout' in kw:
cargs['timeout'] = kw['timeout']
if kw['timeout'] is not None:
if kw['shell']:
Logs.warn('Shell commands cannot timeout %r', cmd)
if not 'start_new_session' in kw:
kw['start_new_session'] = True
del kw['timeout']
if 'input' in kw:
if kw['input']:
@ -420,9 +419,8 @@ class Context(ctx):
cargs = {}
if 'timeout' in kw:
cargs['timeout'] = kw['timeout']
if kw['timeout'] is not None:
if kw['shell']:
Logs.warn('Shell commands cannot timeout %r', cmd)
if not 'start_new_session' in kw:
kw['start_new_session'] = True
del kw['timeout']
if 'input' in kw:
if kw['input']:

View File

@ -9,7 +9,7 @@ The portability fixes try to provide a consistent behavior of the Waf API
through Python versions 2.5 to 3.X and across different platforms (win32, linux, etc)
"""
import os, sys, errno, traceback, inspect, re, datetime, platform, base64
import os, sys, errno, traceback, inspect, re, datetime, platform, base64, signal
try:
import cPickle
except ImportError:
@ -865,7 +865,10 @@ def run_regular_process(cmd, kwargs, cargs={}):
try:
out, err = proc.communicate(**cargs)
except TimeoutExpired:
proc.kill()
try:
os.killpg(proc.pid, signal.SIGKILL)
except AttributeError:
proc.kill()
out, err = proc.communicate()
raise TimeoutExpired(proc.args, timeout=cargs['timeout'], output=out, stderr=err)
status = proc.returncode
@ -874,7 +877,10 @@ def run_regular_process(cmd, kwargs, cargs={}):
try:
status = proc.wait(**cargs)
except TimeoutExpired as e:
proc.kill()
try:
os.killpg(proc.pid, signal.SIGKILL)
except AttributeError:
proc.kill()
proc.wait()
raise e
return status, out, err

View File

@ -2,7 +2,7 @@
# encoding: utf-8
# Thomas Nagy, 2016 (ita)
import sys, traceback, base64
import os, sys, traceback, base64, signal
try:
import cPickle
except ImportError:
@ -34,7 +34,10 @@ def run():
try:
out, err = proc.communicate(**cargs)
except TimeoutExpired:
proc.kill()
try:
os.killpg(proc.pid, signal.SIGKILL)
except AttributeError:
proc.kill()
out, err = proc.communicate()
raise TimeoutExpired(proc.args, timeout=cargs['timeout'], output=out, stderr=err)
ret = proc.returncode