Merge 3.4 (asyncio)

This commit is contained in:
Victor Stinner 2015-01-14 02:13:51 +01:00
commit e54dd0b92b
3 changed files with 100 additions and 25 deletions

View File

@ -96,8 +96,34 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
def kill(self): def kill(self):
self._proc.kill() self._proc.kill()
def _kill_wait(self):
"""Close pipes, kill the subprocess and read its return status.
Function called when an exception is raised during the creation
of a subprocess.
"""
if self._loop.get_debug():
logger.warning('Exception during subprocess creation, '
'kill the subprocess %r',
self,
exc_info=True)
proc = self._proc
if proc.stdout:
proc.stdout.close()
if proc.stderr:
proc.stderr.close()
if proc.stdin:
proc.stdin.close()
try:
proc.kill()
except ProcessLookupError:
pass
proc.wait()
@coroutine @coroutine
def _post_init(self): def _post_init(self):
try:
proc = self._proc proc = self._proc
loop = self._loop loop = self._loop
if proc.stdin is not None: if proc.stdin is not None:
@ -122,6 +148,9 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
for callback, data in self._pending_calls: for callback, data in self._pending_calls:
self._loop.call_soon(callback, *data) self._loop.call_soon(callback, *data)
self._pending_calls = None self._pending_calls = None
except:
self._kill_wait()
raise
def _call(self, cb, *data): def _call(self, cb, *data):
if self._pending_calls is not None: if self._pending_calls is not None:

View File

@ -60,6 +60,8 @@ class SubprocessStreamProtocol(streams.FlowControlMixin,
protocol=self, protocol=self,
reader=None, reader=None,
loop=self._loop) loop=self._loop)
if not self.waiter.cancelled():
self.waiter.set_result(None) self.waiter.set_result(None)
def pipe_data_received(self, fd, data): def pipe_data_received(self, fd, data):
@ -216,7 +218,11 @@ def create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None,
protocol_factory, protocol_factory,
cmd, stdin=stdin, stdout=stdout, cmd, stdin=stdin, stdout=stdout,
stderr=stderr, **kwds) stderr=stderr, **kwds)
try:
yield from protocol.waiter yield from protocol.waiter
except:
transport._kill_wait()
raise
return Process(transport, protocol, loop) return Process(transport, protocol, loop)
@coroutine @coroutine
@ -232,5 +238,9 @@ def create_subprocess_exec(program, *args, stdin=None, stdout=None,
program, *args, program, *args,
stdin=stdin, stdout=stdout, stdin=stdin, stdout=stdout,
stderr=stderr, **kwds) stderr=stderr, **kwds)
try:
yield from protocol.waiter yield from protocol.waiter
except:
transport._kill_wait()
raise
return Process(transport, protocol, loop) return Process(transport, protocol, loop)

View File

@ -251,6 +251,42 @@ class SubprocessMixin:
self.loop.run_until_complete(cancel_wait()) self.loop.run_until_complete(cancel_wait())
def test_cancel_make_subprocess_transport_exec(self):
@asyncio.coroutine
def cancel_make_transport():
coro = asyncio.create_subprocess_exec(*PROGRAM_BLOCKED,
loop=self.loop)
task = self.loop.create_task(coro)
self.loop.call_soon(task.cancel)
try:
yield from task
except asyncio.CancelledError:
pass
# ignore the log:
# "Exception during subprocess creation, kill the subprocess"
with test_utils.disable_logger():
self.loop.run_until_complete(cancel_make_transport())
def test_cancel_post_init(self):
@asyncio.coroutine
def cancel_make_transport():
coro = self.loop.subprocess_exec(asyncio.SubprocessProtocol,
*PROGRAM_BLOCKED)
task = self.loop.create_task(coro)
self.loop.call_soon(task.cancel)
try:
yield from task
except asyncio.CancelledError:
pass
# ignore the log:
# "Exception during subprocess creation, kill the subprocess"
with test_utils.disable_logger():
self.loop.run_until_complete(cancel_make_transport())
if sys.platform != 'win32': if sys.platform != 'win32':
# Unix # Unix