def register_cleanup_lock_removal(
    lock_path: Path, register: Any = atexit.register
) -> Any:
    """Register a cleanup function for removing a lock, by default on atexit."""
    pid = os.getpid()

    def cleanup_on_exit(lock_path: Path = lock_path, original_pid: int = pid) -> None:
        current_pid = os.getpid()
        if current_pid != original_pid:
            # fork
            return
        try:
            lock_path.unlink()
        except OSError:
            pass

    return register(cleanup_on_exit)
Register a cleanup function for removing a lock, by default on atexit.
src/_pytest/pathlib.py