Ejemplo n.º 1
0
 def _exit(self, *_args, **_kwargs):
     with signals.DeferSignals():
         self.node.TransferCurrentProcess()
         if self.run_kill:
             self.child.KillProcesses(remove=True,
                                      sigterm_timeout=self.sigterm_timeout)
         else:
             # Non-strict since the group may have failed to be created.
             self.child.RemoveThisGroup(strict=False)
Ejemplo n.º 2
0
    def ContainChildren(self, pool_name=None, sigterm_timeout=10):
        """Context manager for containing children processes.

    This manager creates a job pool derived from this instance, transfers
    the current process into it upon __enter__.

    Any children processes created at that point will inherit our cgroup;
    they can only escape the group if they're running as root and move
    themselves out of this hierarchy.

    Upon __exit__, transfer the current process back to this group, then
    sigterm (progressing to sigkill) any immediate children in the pool,
    finally removing the pool if possible.

    If pool_name is given, that name is used rather than os.getpid() for
    the job pool created.

    Finally, note that during cleanup this will suppress SIGINT and SIGTERM
    to ensure that it cleanses any children before returning.
    """

        if pool_name is None:
            pool_name = str(os.getpid())

        run_kill = False
        try:
            # Note; we use lazy init here so that we cannot trigger a
            # _GroupWasRemoved; we want that contained.
            node = self.AddGroup(pool_name, autoclean=True, lazy_init=True)
            try:
                node.TransferCurrentProcess()
            except _GroupWasRemoved:
                raise SystemExit(
                    "Group %s was removed under our feet; pool shutdown is underway"
                    % node.namespace)
            run_kill = True
            yield
        finally:
            with signals.DeferSignals():
                self.TransferCurrentProcess()
                if run_kill:
                    node.KillProcesses(remove=True,
                                       sigterm_timeout=sigterm_timeout)
                else:
                    # Non strict since the group may have failed to be created.
                    node.RemoveThisGroup(strict=False)