code: def main(fd, verbose=0):
      """Run resource tracker."""
      # protect the process from ^C and "killall python" etc
      if verbose:
          util.log_to_stderr(level=util.DEBUG)
  
      signal.signal(signal.SIGINT, signal.SIG_IGN)
      signal.signal(signal.SIGTERM, signal.SIG_IGN)
  
      if _HAVE_SIGMASK:
          signal.pthread_sigmask(signal.SIG_UNBLOCK, _IGNORED_SIGNALS)
  
      for f in (sys.stdin, sys.stdout):
          try:
              f.close()
          except Exception:
              pass
  
      if verbose:
          util.debug("Main resource tracker is running")
  
      registry = {rtype: {} for rtype in _CLEANUP_FUNCS.keys()}
      try:
          # keep track of registered/unregistered resources
          if sys.platform == "win32":
              fd = msvcrt.open_osfhandle(fd, os.O_RDONLY)
          with open(fd, "rb") as f:
              while True:
                  line = f.readline()
                  if line == b"":  # EOF
                      break
                  try:
                      splitted = line.strip().decode("ascii").split(":")
                      # name can potentially contain separator symbols (for
                      # instance folders on Windows)
                      cmd, name, rtype = (
                          splitted[0],
                          ":".join(splitted[1:-1]),
                          splitted[-1],
                      )
  
                      if cmd == "PROBE":
                          continue
  
                      if rtype not in _CLEANUP_FUNCS:
                          raise ValueError(
                              f"Cannot register {name} for automatic cleanup: "
                              f"unknown resource type ({rtype}). Resource type "
                              "should be one of the following: "
                              f"{list(_CLEANUP_FUNCS.keys())}"
                          )
  
                      if cmd == "REGISTER":
                          if name not in registry[rtype]:
                              registry[rtype][name] = 1
                          else:
                              registry[rtype][name] += 1
  
                          if verbose:
                              util.debug(
                                  "[ResourceTracker] incremented refcount of "
                                  f"{rtype} {name} "
                                  f"(current {registry[rtype][name]})"
                              )
                      elif cmd == "UNREGISTER":
                          del registry[rtype][name]
                          if verbose:
                              util.debug(
                                  f"[ResourceTracker] unregister {name} {rtype}: "
                                  f"registry({len(registry)})"
                              )
                      elif cmd == "MAYBE_UNLINK":
                          registry[rtype][name] -= 1
                          if verbose:
                              util.debug(
                                  "[ResourceTracker] decremented refcount of "
                                  f"{rtype} {name} "
                                  f"(current {registry[rtype][name]})"
                              )
  
                          if registry[rtype][name] == 0:
                              del registry[rtype][name]
                              try:
                                  if verbose:
                                      util.debug(
                                          f"[ResourceTracker] unlink {name}"
                                      )
                                  _CLEANUP_FUNCS[rtype](name)
                              except Exception as e:
                                  warnings.warn(
                                      f"resource_tracker: {name}: {e!r}"
                                  )
  
                      else:
                          raise RuntimeError(
                              f"unrecognized command {cmd!r}"
                          )
                  except BaseException:
                      try:
                          sys.excepthook(*sys.exc_info())
                      except BaseException:
                          pass
      finally:
          # all processes have terminated; cleanup any remaining resources
          def _unlink_resources(rtype_registry, rtype):
              if rtype_registry:
                  try:
                      warnings.warn(
                          "resource_tracker: There appear to be "
                          f"{len(rtype_registry)} leaked {rtype} objects to "
                          "clean up at shutdown"
                      )
                  except Exception:
                      pass
              for name in rtype_registry:
                  # For some reason the process which created and registered this
                  # resource has failed to unregister it. Presumably it has
                  # died.  We therefore clean it up.
                  try:
                      _CLEANUP_FUNCS[rtype](name)
                      if verbose:
                          util.debug(f"[ResourceTracker] unlink {name}")
                  except Exception as e:
                      warnings.warn(f"resource_tracker: {name}: {e!r}")
  
          for rtype, rtype_registry in registry.items():
              if rtype == "folder":
                  continue
              else:
                  _unlink_resources(rtype_registry, rtype)
  
          # The default cleanup routine for folders deletes everything inside
          # those folders recursively, which can include other resources tracked
          # by the resource tracker). To limit the risk of the resource tracker
          # attempting to delete twice a resource (once as part of a tracked
          # folder, and once as a resource), we delete the folders after all
          # other resource types.
          if "folder" in registry:
              _unlink_resources(registry["folder"], "folder")
  
      if verbose:
          util.debug("resource tracker shut down")
  
file_path: catboost/contrib/python/joblib/joblib/externals/loky/backend/resource_tracker.py
imports: import os
  import shutil
  import sys
  import signal
  import warnings
  import threading
  from _multiprocessing import sem_unlink
  from multiprocessing import util
  from .None import spawn
label_desc: Use image node_id 1 for calling a global function with example usage: main(fd, verbose) without return types