# Buggy code
def start_new_processes(self):
    """Start more processors if we have enough slots and files to process"""
    while self._parallelism - len(self._processors) > 0 and self._file_path_queue:
        file_path = self._file_path_queue.pop(0)
        callback_to_execute_for_file = self._callback_to_execute[file_path]
        processor = self._processor_factory(
            file_path, callback_to_execute_for_file, self._dag_ids, self._pickle_dags
        )

        del self._callback_to_execute[file_path]
        Stats.incr("dag_processing.processes")

        processor.start()
        self.log.debug(
            "Started a process (PID: %s) to generate tasks for %s",
            processor.pid,
            file_path,
        )
        self._processors[file_path] = processor
        self.waitables[processor.waitable_handle] = processor


# Traceback (KeyError)
[2020-12-13 19:35:33,752] {dagbag.py:440} INFO - Filling up the DagBag from /usr/local/lib/python3.8/site-packages/airflow/example_dags/example_bash_operator.py
Running <TaskInstance: example_bash_operator.run_after_loop 2020-12-13T19:35:30.648020+00:00 [queued]> on host 6611da4b1a27
[2020-12-13 19:35:34,517] {dagrun.py:444} INFO - Marking run <DagRun example_bash_operator @ 2020-12-13 19:35:30.648020+00:00: manual__2020-12-13T19:35:30.648020+00:00, externally triggered: True> successful
[2020-12-13 19:35:34,523] {scheduler_job.py:1193} INFO - Executor reports execution of example_bash_operator.run_after_loop execution_date=2020-12-13 19:35:30.648020+00:00 exited with status success for try_number 1
Process ForkProcess-34:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/local/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.8/site-packages/airflow/utils/dag_processing.py", line 365, in _run_processor_manager
processor_manager.start()
File "/usr/local/lib/python3.8/site-packages/airflow/utils/dag_processing.py", line 596, in start
return self._run_parsing_loop()
File "/usr/local/lib/python3.8/site-packages/airflow/utils/dag_processing.py", line 659, in _run_parsing_loop
self._processors.pop(processor.file_path)
KeyError: '/usr/local/lib/python3.8/site-packages/airflow/example_dags/example_bash_operator.py'
[2020-12-13 19:35:35,589] {dag_processing.py:396} WARNING - DagFileProcessorManager (PID=1029759) exited with exit code 1 - re-launching

# Fixed code
def start_new_processes(self):
    """Start more processors if we have enough slots and files to process"""
    while self._parallelism - len(self._processors) > 0 and self._file_path_queue:
        file_path = self._file_path_queue.pop(0)
        # Stop creating duplicate processor i.e. processor with the same filepath
        if file_path in self._processors.keys():
            continue

        callback_to_execute_for_file = self._callback_to_execute[file_path]
        processor = self._processor_factory(
            file_path, callback_to_execute_for_file, self._dag_ids, self._pickle_dags
        )

        del self._callback_to_execute[file_path]
        Stats.incr("dag_processing.processes")

        processor.start()
        self.log.debug(
            "Started a process (PID: %s) to generate tasks for %s",
            processor.pid,
            file_path,
        )
        self._processors[file_path] = processor
        self.waitables[processor.waitable_handle] = processor