# Buggy code
def _execute(self, session=None):
    """
    Initializes all components required to run a dag for a specified date range and
    calls helper method to execute the tasks.
    """
    ti_status = BackfillJob._DagRunTaskStatus()

    start_date = self.bf_start_date

    # Get intervals between the start/end dates, which will turn into dag runs
    run_dates = self.dag.get_run_dates(start_date=start_date, end_date=self.bf_end_date)
    if self.run_backwards:
        tasks_that_depend_on_past = [
            t.task_id for t in self.dag.task_dict.values() if t.depends_on_past
        ]
        if tasks_that_depend_on_past:
            raise AirflowException(
                "You cannot backfill backwards because one or more tasks depend_on_past: {}".format(
                    ",".join(tasks_that_depend_on_past)
                )
            )
        run_dates = run_dates[::-1]

    if len(run_dates) == 0:
        self.log.info("No run dates were found for the given dates and dag interval.")
        return

    # picklin'
    pickle_id = None

    if not self.donot_pickle and self.executor_class not in (
        executor_constants.LOCAL_EXECUTOR,
        executor_constants.SEQUENTIAL_EXECUTOR,
        executor_constants.DASK_EXECUTOR,
    ):
        pickle = DagPickle(self.dag)
        session.add(pickle)
        session.commit()
        pickle_id = pickle.id

    executor = self.executor
    executor.start()

    ti_status.total_runs = len(run_dates)  # total dag runs in backfill

    try:  # pylint: disable=too-many-nested-blocks
        remaining_dates = ti_status.total_runs
        while remaining_dates > 0:
            dates_to_process = [
                run_date
                for run_date in run_dates
                if run_date not in ti_status.executed_dag_run_dates
            ]

            self._execute_for_run_dates(
                run_dates=dates_to_process,
                ti_status=ti_status,
                executor=executor,
                pickle_id=pickle_id,
                start_date=start_date,
                session=session,
            )

            remaining_dates = ti_status.total_runs - len(
                ti_status.executed_dag_run_dates
            )
            err = self._collect_errors(ti_status=ti_status, session=session)
            if err:
                raise BackfillUnfinished(err, ti_status)

            if remaining_dates > 0:
                self.log.info(
                    "max_active_runs limit for dag %s has been reached "
                    " - waiting for other dag runs to finish",
                    self.dag_id,
                )
                time.sleep(self.delay_on_limit_secs)
    except (KeyboardInterrupt, SystemExit):
        self.log.warning("Backfill terminated by user.")

        # TODO: we will need to terminate running task instances and set the
        # state to failed.
        self._set_unfinished_dag_runs_to_failed(ti_status.active_runs)
    finally:
        session.commit()
        executor.end()

    self.log.info("Backfill done. Exiting.")


# Traceback (airflow.exceptions.AirflowException)
Something bad has happened.
Please consider letting us know by creating a bug report using GitHub.

Python version: 3.8.7
Airflow version: 2.0.0
Node: airflow-web-ffdd89d6-h98vj
-------------------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.8/site-packages/airflow/www/auth.py", line 34, in decorated
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/airflow/www/decorators.py", line 60, in wrapper
return f(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/airflow/www/views.py", line 1366, in run
executor.start()
File "/usr/local/lib/python3.8/site-packages/airflow/executors/kubernetes_executor.py", line 493, in start
raise AirflowException("Could not get scheduler_job_id")
airflow.exceptions.AirflowException: Could not get scheduler_job_id

# Fixed code
def _execute(self, session=None):
    """
    Initializes all components required to run a dag for a specified date range and
    calls helper method to execute the tasks.
    """
    ti_status = BackfillJob._DagRunTaskStatus()

    start_date = self.bf_start_date

    # Get intervals between the start/end dates, which will turn into dag runs
    run_dates = self.dag.get_run_dates(start_date=start_date, end_date=self.bf_end_date)
    if self.run_backwards:
        tasks_that_depend_on_past = [
            t.task_id for t in self.dag.task_dict.values() if t.depends_on_past
        ]
        if tasks_that_depend_on_past:
            raise AirflowException(
                "You cannot backfill backwards because one or more tasks depend_on_past: {}".format(
                    ",".join(tasks_that_depend_on_past)
                )
            )
        run_dates = run_dates[::-1]

    if len(run_dates) == 0:
        self.log.info("No run dates were found for the given dates and dag interval.")
        return

    # picklin'
    pickle_id = None

    if not self.donot_pickle and self.executor_class not in (
        executor_constants.LOCAL_EXECUTOR,
        executor_constants.SEQUENTIAL_EXECUTOR,
        executor_constants.DASK_EXECUTOR,
    ):
        pickle = DagPickle(self.dag)
        session.add(pickle)
        session.commit()
        pickle_id = pickle.id

    executor = self.executor
    executor.job_id = "backfill"
    executor.start()

    ti_status.total_runs = len(run_dates)  # total dag runs in backfill

    try:  # pylint: disable=too-many-nested-blocks
        remaining_dates = ti_status.total_runs
        while remaining_dates > 0:
            dates_to_process = [
                run_date
                for run_date in run_dates
                if run_date not in ti_status.executed_dag_run_dates
            ]

            self._execute_for_run_dates(
                run_dates=dates_to_process,
                ti_status=ti_status,
                executor=executor,
                pickle_id=pickle_id,
                start_date=start_date,
                session=session,
            )

            remaining_dates = ti_status.total_runs - len(
                ti_status.executed_dag_run_dates
            )
            err = self._collect_errors(ti_status=ti_status, session=session)
            if err:
                raise BackfillUnfinished(err, ti_status)

            if remaining_dates > 0:
                self.log.info(
                    "max_active_runs limit for dag %s has been reached "
                    " - waiting for other dag runs to finish",
                    self.dag_id,
                )
                time.sleep(self.delay_on_limit_secs)
    except (KeyboardInterrupt, SystemExit):
        self.log.warning("Backfill terminated by user.")

        # TODO: we will need to terminate running task instances and set the
        # state to failed.
        self._set_unfinished_dag_runs_to_failed(ti_status.active_runs)
    finally:
        session.commit()
        executor.end()

    self.log.info("Backfill done. Exiting.")