Skip to content

Commit 86dac6d

Browse files
committed
Add a subprocess.Popen timeout when possible
The linux timeout command seems to fail terminating processes occasionally, so when possible without breaking the interface I've added a subprocess.Popen timeout (to be double the linux timeout) as a backup timeout. This is a bit hackish but has been useful for me.
1 parent 47903f6 commit 86dac6d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

afl-cov

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def run_cmd(
856856
collect: int,
857857
aflrun: bool,
858858
fn: str,
859-
timeout: Optional[int] = None,
859+
timeout: Optional[str] = None,
860860
) -> Tuple[int, List[bytes]]:
861861
out = []
862862

@@ -877,9 +877,22 @@ def run_cmd(
877877
if timeout:
878878
cmd = "timeout -s KILL %s %s" % (timeout, cmd)
879879

880-
exit_code = subprocess.call(
881-
cmd, stdin=None, stdout=fh, stderr=subprocess.STDOUT, shell=True
882-
)
880+
# If timeout is in seconds, add an extra layer of timeout
881+
# the timeout linux command failed me on several occasions
882+
sp_timeout = int(timeout) * 2 if timeout and timeout.isdigit() else None
883+
884+
try:
885+
exit_code = subprocess.call(
886+
cmd, stdin=None, stdout=fh, stderr=subprocess.STDOUT, shell=True,
887+
timeout=sp_timeout
888+
)
889+
except subprocess.TimeoutExpired:
890+
if cargs.verbose:
891+
if log_file:
892+
logr(b" CMD: %s timedout: %s" % (cmd.encode(), str(ex)), log_file, cargs)
893+
else:
894+
print(" CMD: %s timedout: %s" % (cmd, str(ex)))
895+
exit_code = -1
883896

884897
fh.close()
885898

0 commit comments

Comments
 (0)