Fight against zombies
This commit is contained in:
parent
30de38aa1e
commit
cd6197caa3
37
runall.py
37
runall.py
|
@ -1,26 +1,31 @@
|
|||
from itertools import product
|
||||
import subprocess
|
||||
import signal
|
||||
from subprocess import DEVNULL, PIPE, Popen, TimeoutExpired
|
||||
|
||||
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ '
|
||||
|
||||
# Prevent zombie processes
|
||||
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
|
||||
|
||||
|
||||
def check_pair(script, instr, outstr):
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"/usr/bin/env", "bash", "-c",
|
||||
";\n".join((
|
||||
# Cleanup child processes on exit
|
||||
"trap 'kill -9 $(jobs -p)' SIGINT SIGTERM EXIT",
|
||||
script,
|
||||
))
|
||||
],
|
||||
timeout=5,
|
||||
input=instr.encode(),
|
||||
capture_output=True,
|
||||
process = Popen(
|
||||
["/usr/bin/env", "bash", "-c", "--", script],
|
||||
stdin=PIPE,
|
||||
stdout=PIPE,
|
||||
stderr=DEVNULL,
|
||||
)
|
||||
return result.returncode == 0 and result.stdout == outstr.encode()
|
||||
except subprocess.TimeoutExpired:
|
||||
|
||||
try:
|
||||
stdout, stderr = process.communicate(instr.encode(), timeout=5)
|
||||
return process.returncode == 0 and stdout == outstr.encode()
|
||||
except TimeoutExpired:
|
||||
try:
|
||||
process.kill()
|
||||
stdout, stderr = process.communicate()
|
||||
except ProcessLookupError:
|
||||
pass
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue