Example #1
0
  def _IncomingConnection(self):
    """Called for each incoming connection

    """
    # pylint: disable=W0212
    (connection, client_addr) = self.socket.accept()

    self._CollectChildren(False)

    pid = os.fork()
    if pid == 0:
      # Child process
      try:
        # The client shouldn't keep the listening socket open. If the parent
        # process is restarted, it would fail when there's already something
        # listening (in this case its own child from a previous run) on the
        # same port.
        try:
          self.socket.close()
        except socket.error:
          pass
        self.socket = None

        # In case the handler code uses temporary files
        utils.ResetTempfileModule()

        self.request_executor(self, self.handler, connection, client_addr)
      except Exception: # pylint: disable=W0703
        logging.exception("Error while handling request from %s:%s",
                          client_addr[0], client_addr[1])
        os._exit(1)
      os._exit(0)
    else:
      self._children.append(pid)
Example #2
0
    def _IncomingConnection(self):
        """Called for each incoming connection

    """
        # pylint: disable=W0212
        t_start = time.time()
        (connection, client_addr) = self.socket.accept()

        self._CollectChildren(False)

        try:
            pid = os.fork()
        except OSError:
            logging.exception("Failed to fork on request from %s:%s",
                              client_addr[0], client_addr[1])
            # Immediately close the connection. No SSL handshake has been done.
            try:
                connection.close()
            except socket.error:
                pass
            return

        if pid == 0:
            # Child process
            try:
                # The client shouldn't keep the listening socket open. If the parent
                # process is restarted, it would fail when there's already something
                # listening (in this case its own child from a previous run) on the
                # same port.
                try:
                    self.socket.close()
                except socket.error:
                    pass
                self.socket = None

                # In case the handler code uses temporary files
                utils.ResetTempfileModule()

                t_setup = time.time()
                self.request_executor(self, self.handler, connection,
                                      client_addr)
                t_end = time.time()
                logging.debug(
                    "Request from %s:%s executed in: %.4f [setup: %.4f] "
                    "[workers: %d]", client_addr[0], client_addr[1],
                    t_end - t_start, t_setup - t_start, len(self._children))

            except Exception:  # pylint: disable=W0703
                logging.exception("Error while handling request from %s:%s",
                                  client_addr[0], client_addr[1])
                os._exit(1)
            os._exit(0)
        else:
            self._children.append(pid)
Example #3
0
    def _Test(self, reset):
        self.failIf(tempfile.TMP_MAX > 10)

        # Initialize tempfile module
        (fd, _) = tempfile.mkstemp(dir=self.tmpdir, prefix="init.", suffix="")
        os.close(fd)

        (notify_read, notify_write) = os.pipe()

        pid = os.fork()
        if pid == 0:
            # Child
            try:
                try:
                    if reset:
                        utils.ResetTempfileModule()

                    os.close(notify_write)

                    # Wait for parent to close pipe
                    os.read(notify_read, 1)

                    try:
                        # This is a short-lived process, not caring about closing file
                        # descriptors
                        (_, path) = tempfile.mkstemp(dir=self.tmpdir,
                                                     prefix="test.",
                                                     suffix="")
                    except EnvironmentError, err:
                        if err.errno == errno.EEXIST:
                            # Couldnt' create temporary file (e.g. because we run out of
                            # retries)
                            os._exit(2)
                        raise

                    logging.debug("Child created %s", path)

                    os._exit(0)
                except Exception:
                    logging.exception("Unhandled error")
            finally:
Example #4
0
  def _Test(self, reset):
    self.assertFalse(tempfile.TMP_MAX > 10)

    # Initialize tempfile module
    (fd, _) = tempfile.mkstemp(dir=self.tmpdir, prefix="init.", suffix="")
    os.close(fd)

    (notify_read, notify_write) = os.pipe()

    pid = os.fork()
    if pid == 0:
      # Child
      try:
        try:
          if reset:
            utils.ResetTempfileModule()

          os.close(notify_write)

          # Wait for parent to close pipe
          os.read(notify_read, 1)

          try:
            # This is a short-lived process, not caring about closing file
            # descriptors
            (_, path) = tempfile.mkstemp(dir=self.tmpdir,
                                         prefix="test.", suffix="")
          except EnvironmentError as err:
            if err.errno == errno.EEXIST:
              # Couldnt' create temporary file (e.g. because we run out of
              # retries)
              os._exit(2)
            raise

          logging.debug("Child created %s", path)

          os._exit(0)
        except Exception:
          logging.exception("Unhandled error")
      finally:
        os._exit(1)

    # Parent
    os.close(notify_read)

    # Create parent's temporary files
    for _ in range(tempfile.TMP_MAX):
      (fd, path) = tempfile.mkstemp(dir=self.tmpdir,
                                    prefix="test.", suffix="")
      os.close(fd)
      logging.debug("Parent created %s", path)

    # Notify child by closing pipe
    os.close(notify_write)

    (_, status) = os.waitpid(pid, 0)

    self.assertFalse(os.WIFSIGNALED(status))

    if reset:
      # If the tempfile module was reset, it should not fail to create
      # temporary files
      expected = 0
    else:
      expected = 2

    self.assertEqual(os.WEXITSTATUS(status), expected)