code: def _pipe_stdio(cls, sock, stdin_isatty, stdout_isatty, stderr_isatty, handle_stdin):
      
      stdio_writers = (
        (ChunkType.STDOUT, stdout_isatty),
        (ChunkType.STDERR, stderr_isatty)
      )
      types, ttys = zip(*(stdio_writers))
  
      @contextmanager
      def maybe_handle_stdin(want):
        if want:
          # TODO: Launching this thread pre-fork to handle @rule input currently results
          # in an unhandled SIGILL in `src/python/pants/engine/scheduler.py, line 313 in pre_fork`.
          # More work to be done here in https://github.com/pantsbuild/pants/issues/6005
          with NailgunStreamStdinReader.open(sock, stdin_isatty) as fd:
            yield fd
        else:
          with open('/dev/null', 'rb') as fh:
            yield fh.fileno()
  
      with maybe_handle_stdin(handle_stdin) as stdin_fd,\
           NailgunStreamWriter.open_multi(sock, types, ttys) as ((stdout_fd, stderr_fd), writer),\
           stdio_as(stdout_fd=stdout_fd, stderr_fd=stderr_fd, stdin_fd=stdin_fd):
        # N.B. This will be passed to and called by the `DaemonExiter` prior to sending an
        # exit chunk, to avoid any socket shutdown vs write races.
        stdout, stderr = sys.stdout, sys.stderr
        def finalizer():
          try:
            stdout.flush()
            stderr.flush()
          finally:
            time.sleep(.001)  # HACK: Sleep 1ms in the main thread to free the GIL.
            writer.stop()
            writer.join()
            stdout.close()
            stderr.close()
        yield finalizer
docs: Handles stdio redirection in the case of pipes and/or mixed pipes and ttys.