def clean_up_previous_tunnel_pids()

in src/watchdog/__init__.py [0:0]


def clean_up_previous_tunnel_pids(state_file_dir=STATE_FILE_DIR):
    """
    Cleans up efs-proxy/stunnel pids created by mount watchdog spawned by a previous efs-csi-driver
    pod after driver restart, upgrade, or crash. This method attempts to clean PIDs from persisted
    state files after efs-csi-driver restart to ensure watchdog creates a new tunnel.
    """
    state_files = get_state_files(state_file_dir)
    logging.debug(
        'Persisted state files in "%s": %s', state_file_dir, list(state_files.values())
    )

    for state_file in state_files.values():
        state_file_path = os.path.join(state_file_dir, state_file)
        with open(state_file_path) as f:
            try:
                state = json.load(f)
            except ValueError:
                logging.exception("Unable to parse json in %s", state_file_path)
                continue

            try:
                pid = state["pid"]
            except KeyError:
                logging.debug("No PID found in state file %s", state_file)
                continue

            out = check_process_name(pid)

            if out and ("stunnel" in str(out) or "efs-proxy" in str(out)):
                logging.debug(
                    "PID %s in state file %s is active. Skipping clean up",
                    pid,
                    state_file,
                )
                continue

            state.pop("pid")
            logging.debug("Cleaning up pid %s in state file %s", pid, state_file)

            rewrite_state_file(state, state_file_dir, state_file)