def stop(self):
        """Stop a task immediately.

        Raises:
            RuntimeError: If the task hasn't been started or has already been
                stopped.
        """
        if self._status is TaskStatus.STOPPED:
            return

        if self._status is not TaskStatus.STARTED:
            raise RuntimeError("Cannot stop %s in state %s" %
                               (self, self._status))
        self._stop()

        STARTED_TASKS.remove(self)
        self._status = TaskStatus.STOPPED