code: def ensure_dir_exists(path: str | Path, mode: int = 0o777) -> None:
      """Ensure that a directory exists
  
      If it doesn't exist, try to create it, protecting against a race condition
      if another process is doing the same.
      The default permissions are determined by the current umask.
      """
      try:
          Path(path).mkdir(parents=True, mode=mode)
      except OSError as e:
          if e.errno != errno.EEXIST:
              raise
      if not Path(path).is_dir():
          raise OSError("%r exists but is not a directory" % path)
  
imports: from __future__ import annotations
  import asyncio
  import atexit
  import errno
  import inspect
  import sys
  import threading
  import warnings
  from pathlib import Path
  from types import FrameType
  from typing import Any, Awaitable, Callable, TypeVar, cast
label_desc: Use image node_id 1 for calling a global function with example usage: ensure_dir_exists(path, mode) without return types