import select
import time
+from portage import os
from portage.util import writemsg_level
from _emerge.SlotObject import SlotObject
class PollScheduler(object):
class _sched_iface_class(SlotObject):
- __slots__ = ("register", "schedule", "unregister")
+ __slots__ = ("register", "schedule", "schedule_waitpid", "unregister")
def __init__(self):
self._max_jobs = 1
return event_handled
+ def _schedule_waitpid(self, pid):
+ """
+ Schedule until waitpid returns process status
+ for the given pid, and return the result from waitpid.
+ This is meant to be called as a last resort, since
+ it won't return until the process exits. This can raise
+ OSError from the waitpid call (typically errno.ECHILD).
+ @type pid: int
+ @param pid: the pid of the child process to wait for
+ """
+ event_handlers = self._poll_event_handlers
+
+ try:
+ while event_handlers:
+ f, event = self._next_poll_event()
+ try:
+ handler, reg_id = event_handlers[f]
+ except KeyError:
+ pass
+ else:
+ handler(f, event)
+ wait_retval = os.waitpid(pid, os.WNOHANG)
+ if wait_retval != (0, 0):
+ return wait_retval
+ self.schedule()
+ except StopIteration:
+ pass
+
+ # Once scheduling is exhaused, do a blocking waitpid.
+ return os.waitpid(pid, 0)
_can_poll_device = None
self.sched_iface = self._sched_iface_class(
register=self._register,
schedule=self._schedule_wait,
- unregister=self._unregister)
+ unregister=self._unregister,
+ schedule_waitpid=self._schedule_waitpid)
self._queues = []
self._schedule_listeners = []
class _iface_class(SlotObject):
__slots__ = ("dblinkEbuildPhase", "dblinkDisplayMerge",
"dblinkElog", "dblinkEmergeLog", "fetch", "register", "schedule",
+ "schedule_waitpid",
"scheduleSetup", "scheduleUnpack", "scheduleYield",
"unregister")
dblinkEmergeLog=self._dblink_emerge_log,
fetch=fetch_iface, register=self._register,
schedule=self._schedule_wait,
+ schedule_waitpid=self._schedule_waitpid,
scheduleSetup=self._schedule_setup,
scheduleUnpack=self._schedule_unpack,
scheduleYield=self._schedule_yield,
return self.returncode
try:
- wait_retval = os.waitpid(self.pid, 0)
+ wait_retval = os.waitpid(self.pid, os.WNOHANG)
except OSError as e:
if e.errno != errno.ECHILD:
raise
del e
self._set_returncode((self.pid, 1))
else:
- self._set_returncode(wait_retval)
+ if wait_retval != (0, 0):
+ self._set_returncode(wait_retval)
+ else:
+ try:
+ wait_retval = self.scheduler.schedule_waitpid(self.pid)
+ except OSError as e:
+ if e.errno != errno.ECHILD:
+ raise
+ del e
+ self._set_returncode((self.pid, 1))
+ else:
+ self._set_returncode(wait_retval)
return self.returncode