1 |
commit: 8074127bbc213fde75d51309c8fb4ee33ad278aa |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Feb 29 08:33:46 2020 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Feb 29 08:34:34 2020 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=8074127b |
7 |
|
8 |
SpawnProcess: add _main coroutine |
9 |
|
10 |
Add a _main coroutine method and make the PipeLogger instance |
11 |
a local variable. |
12 |
|
13 |
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org> |
14 |
|
15 |
lib/_emerge/SpawnProcess.py | 33 ++++++++++++++++++++++----------- |
16 |
1 file changed, 22 insertions(+), 11 deletions(-) |
17 |
|
18 |
diff --git a/lib/_emerge/SpawnProcess.py b/lib/_emerge/SpawnProcess.py |
19 |
index ba58d9d0e..cda615ded 100644 |
20 |
--- a/lib/_emerge/SpawnProcess.py |
21 |
+++ b/lib/_emerge/SpawnProcess.py |
22 |
@@ -20,6 +20,7 @@ from portage.localization import _ |
23 |
from portage.output import EOutput |
24 |
from portage.util import writemsg_level |
25 |
from portage.util._async.PipeLogger import PipeLogger |
26 |
+from portage.util.futures import asyncio |
27 |
from portage.util.futures.compat_coroutine import coroutine |
28 |
|
29 |
class SpawnProcess(SubProcess): |
30 |
@@ -36,7 +37,7 @@ class SpawnProcess(SubProcess): |
31 |
"unshare_ipc", "unshare_mount", "unshare_pid", "unshare_net") |
32 |
|
33 |
__slots__ = ("args",) + \ |
34 |
- _spawn_kwarg_names + ("_pipe_logger", "_selinux_type",) |
35 |
+ _spawn_kwarg_names + ("_main_task", "_selinux_type",) |
36 |
|
37 |
# Max number of attempts to kill the processes listed in cgroup.procs, |
38 |
# given that processes may fork before they can be killed. |
39 |
@@ -141,13 +142,28 @@ class SpawnProcess(SubProcess): |
40 |
fcntl.fcntl(stdout_fd, |
41 |
fcntl.F_GETFD) | fcntl.FD_CLOEXEC) |
42 |
|
43 |
- self._pipe_logger = PipeLogger(background=self.background, |
44 |
+ pipe_logger = PipeLogger(background=self.background, |
45 |
scheduler=self.scheduler, input_fd=master_fd, |
46 |
log_file_path=log_file_path, |
47 |
stdout_fd=stdout_fd) |
48 |
- self._pipe_logger.addExitListener(self._pipe_logger_exit) |
49 |
self._registered = True |
50 |
- yield self._pipe_logger.async_start() |
51 |
+ yield pipe_logger.async_start() |
52 |
+ |
53 |
+ self._main_task = asyncio.ensure_future( |
54 |
+ self._main(pipe_logger), loop=self.scheduler) |
55 |
+ self._main_task.add_done_callback(self._main_exit) |
56 |
+ |
57 |
+ @coroutine |
58 |
+ def _main(self, pipe_logger): |
59 |
+ if pipe_logger.poll() is None: |
60 |
+ yield pipe_logger.async_wait() |
61 |
+ |
62 |
+ def _main_exit(self, main_task): |
63 |
+ try: |
64 |
+ main_task.result() |
65 |
+ except asyncio.CancelledError: |
66 |
+ self.cancel() |
67 |
+ self._async_waitpid() |
68 |
|
69 |
def _can_log(self, slave_fd): |
70 |
return True |
71 |
@@ -171,21 +187,16 @@ class SpawnProcess(SubProcess): |
72 |
|
73 |
return spawn_func(args, **kwargs) |
74 |
|
75 |
- def _pipe_logger_exit(self, pipe_logger): |
76 |
- self._pipe_logger = None |
77 |
- self._async_waitpid() |
78 |
- |
79 |
def _unregister(self): |
80 |
SubProcess._unregister(self) |
81 |
if self.cgroup is not None: |
82 |
self._cgroup_cleanup() |
83 |
self.cgroup = None |
84 |
- if self._pipe_logger is not None: |
85 |
- self._pipe_logger.cancel() |
86 |
- self._pipe_logger = None |
87 |
|
88 |
def _cancel(self): |
89 |
SubProcess._cancel(self) |
90 |
+ if self._main_task is not None: |
91 |
+ self._main_task.cancel() |
92 |
self._cgroup_cleanup() |
93 |
|
94 |
def _cgroup_cleanup(self): |