code: def stop_standing_subprocess(proc):
      
      # Only import psutil when actually needed.
      # psutil may cause import error in certain env. This way the utils module
      # doesn't crash upon import.
      import psutil
      pid = proc.pid
      logging.debug('Stopping standing subprocess %d', pid)
      process = psutil.Process(pid)
      failed = []
      try:
          children = process.children(recursive=True)
      except AttributeError:
          # Handle versions <3.0.0 of psutil.
          children = process.get_children(recursive=True)
      for child in children:
          try:
              child.kill()
              child.wait(timeout=10)
          except psutil.NoSuchProcess:
              # Ignore if the child process has already terminated.
              pass
          except:
              failed.append(child.pid)
              logging.exception('Failed to kill standing subprocess %d',
                                child.pid)
      try:
          process.kill()
          process.wait(timeout=10)
      except psutil.NoSuchProcess:
          # Ignore if the process has already terminated.
          pass
      except:
          failed.append(pid)
          logging.exception('Failed to kill standing subprocess %d', pid)
      if failed:
          raise Error('Failed to kill standing subprocesses: %s' % failed)
      # Call wait and close pipes on the original Python object so we don't get
      # runtime warnings.
      if proc.stdout:
          proc.stdout.close()
      if proc.stderr:
          proc.stderr.close()
      proc.wait()
      logging.debug('Stopped standing subprocess %d', pid)
docs: Stops a subprocess started by start_standing_subprocess.
  
      Before killing the process, we check if the process is running, if it has
      terminated, Error is raised.
  
      Catches and ignores the PermissionError which only happens on Macs.
  
      Args:
          proc: Subprocess to terminate.
  
      Raises:
          Error: if the subprocess could not be stopped.