# Buggy code
def _CheckStatusAnalysisProcess(self, pid):
    """Checks the status of an analysis process.

    Args:
      pid (int): process ID (PID) of a registered analysis process.

    Raises:
      KeyError: if the process is not registered with the engine.
    """
    # TODO: Refactor this method, simplify and separate concerns (monitoring
    # vs management).
    self._RaiseIfNotRegistered(pid)

    process = self._processes_per_pid[pid]

    process_status = self._GetProcessStatus(process)
    if process_status is None:
        process_is_alive = False
    else:
        process_is_alive = True

    if isinstance(process_status, dict):
        self._rpc_errors_per_pid[pid] = 0
        status_indicator = process_status.get("processing_status", None)

    else:
        rpc_errors = self._rpc_errors_per_pid.get(pid, 0) + 1
        self._rpc_errors_per_pid[pid] = rpc_errors

        if rpc_errors > self._MAXIMUM_RPC_ERRORS:
            process_is_alive = False

        if process_is_alive:
            rpc_port = process.rpc_port.value
            logging.warning(
                (
                    "Unable to retrieve process: {0:s} (PID: {1:d}) status via "
                    "RPC socket: http://localhost:{2:d}"
                ).format(process.name, pid, rpc_port)
            )

            processing_status_string = "RPC error"
            status_indicator = definitions.PROCESSING_STATUS_RUNNING
        else:
            processing_status_string = "killed"
            status_indicator = definitions.PROCESSING_STATUS_KILLED

        process_status = {"processing_status": processing_status_string}

    self._UpdateProcessingStatus(pid, process_status)

    if status_indicator in definitions.PROCESSING_ERROR_STATUS:
        logging.error(
            (
                "Process {0:s} (PID: {1:d}) is not functioning correctly. "
                "Status code: {2!s}."
            ).format(process.name, pid, status_indicator)
        )

        self._TerminateProcess(pid)


# Traceback (KeyError)
[ERROR] (nsrlsvr   ) PID:XXXXX <analysis_process> <dfvfs.path.os_path_spec.OSPathSpec object at 0xXXXXXXXXXXXX>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/plaso/multi_processing/analysis_process.py", line 138, in _Main
self._analysis_mediator.ProduceAnalysisReport(self._analysis_plugin)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/mediator.py", line 95, in ProduceAnalysisReport
analysis_report = plugin.CompileReport(self)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/interface.py", line 280, in CompileReport
hash_analysis)
File "/usr/lib/python2.7/dist-packages/plaso/analysis/interface.py", line 187, in _HandleHashAnalysis
event_uuids = self._event_uuids_by_pathspec.pop(pathspec)
KeyError: <dfvfs.path.os_path_spec.OSPathSpec object at 0xXXXXXXXXXXXX>

# Fixed code
def _CheckStatusAnalysisProcess(self, pid):
    """Checks the status of an analysis process.

    Args:
      pid (int): process ID (PID) of a registered analysis process.

    Raises:
      KeyError: if the process is not registered with the engine.
    """
    # TODO: Refactor this method, simplify and separate concerns (monitoring
    # vs management).
    self._RaiseIfNotRegistered(pid)

    if pid in self._completed_analysis_processes:
        status_indicator = definitions.PROCESSING_STATUS_COMPLETED
        process_status = {"processing_status": status_indicator}

    else:
        process = self._processes_per_pid[pid]

        process_status = self._GetProcessStatus(process)
        if process_status is None:
            process_is_alive = False
        else:
            process_is_alive = True

        if isinstance(process_status, dict):
            self._rpc_errors_per_pid[pid] = 0
            status_indicator = process_status.get("processing_status", None)

            if status_indicator == definitions.PROCESSING_STATUS_COMPLETED:
                self._completed_analysis_processes.add(pid)

        else:
            rpc_errors = self._rpc_errors_per_pid.get(pid, 0) + 1
            self._rpc_errors_per_pid[pid] = rpc_errors

            if rpc_errors > self._MAXIMUM_RPC_ERRORS:
                process_is_alive = False

            if process_is_alive:
                rpc_port = process.rpc_port.value
                logging.warning(
                    (
                        "Unable to retrieve process: {0:s} (PID: {1:d}) status via "
                        "RPC socket: http://localhost:{2:d}"
                    ).format(process.name, pid, rpc_port)
                )

                processing_status_string = "RPC error"
                status_indicator = definitions.PROCESSING_STATUS_RUNNING
            else:
                processing_status_string = "killed"
                status_indicator = definitions.PROCESSING_STATUS_KILLED

            process_status = {"processing_status": processing_status_string}

    self._UpdateProcessingStatus(pid, process_status)

    if status_indicator in definitions.PROCESSING_ERROR_STATUS:
        logging.error(
            (
                "Process {0:s} (PID: {1:d}) is not functioning correctly. "
                "Status code: {2!s}."
            ).format(process.name, pid, status_indicator)
        )

        self._TerminateProcess(pid)