Skip to content

Commit

Permalink
worker: wait for process termination
Browse files Browse the repository at this point in the history
This prevents stray SIGCHLDs from crashing the program e.g. if the asyncio event loop is closed before the process actually terminates.
sbourdeauducq committed Jun 4, 2015
1 parent c843c35 commit a6a4765
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion artiq/master/worker.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@

from artiq.protocols import pyon
from artiq.language.units import strip_unit
from artiq.tools import asyncio_process_wait_timeout, asyncio_wait_or_cancel
from artiq.tools import (asyncio_process_wait_timeout, asyncio_process_wait,
asyncio_wait_or_cancel)


logger = logging.getLogger(__name__)
@@ -101,13 +102,15 @@ def close(self):
logger.warning("failed to send terminate command to worker"
" (RID %d), killing", self.rid, exc_info=True)
self.process.kill()
yield from asyncio_process_wait(self.process)
return
try:
yield from asyncio_process_wait_timeout(self.process,
self.term_timeout)
except asyncio.TimeoutError:
logger.warning("worker did not exit (RID %d), killing", self.rid)
self.process.kill()
yield from asyncio_process_wait(self.process)
else:
logger.debug("worker exited gracefully (RID %d)", self.rid)
finally:
9 changes: 9 additions & 0 deletions artiq/tools.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ def parse_arguments(arguments):
d[name] = pyon.decode(value)
return d


def format_arguments(arguments):
fmtargs = []
for k, v in sorted(arguments.items(), key=itemgetter(0)):
@@ -98,6 +99,14 @@ def asyncio_process_wait_timeout(process, timeout):
timeout=end_time - time.monotonic())


@asyncio.coroutine
def asyncio_process_wait(process):
r = True
while r:
f, p = yield from asyncio.wait([process.stdout.read(1024)])
r = f.pop().result()


@asyncio.coroutine
def asyncio_wait_or_cancel(fs, **kwargs):
fs = [asyncio.async(f) for f in fs]

0 comments on commit a6a4765

Please sign in to comment.