code: def _graceful_finish(self):
          
  
          if not self.process.stdout.closed or not self.process.stderr.closed:
              return # stdout/stderr still open
  
          if not self.process.stdin.closed:
              self.process.stdin.close()
  
          if self.number_of_8k_chunks_sent > 0:
              logger.debug('Sent %d * 8k chunks', self.number_of_8k_chunks_sent)
  
          logger.debug("Finishing up. Process poll: %r", self.process.poll())
  
          if not self.headers_sent:
              retval = self.process.poll()
              if retval != 0:
                  logger.warning("Empty response. Git return value: " + str(retval))
                  payload = "Did not produce any data. Errorcode: " + str(retval)
                  data = 'HTTP/1.1 500 Internal Server Error\r\nDate: %s\r\nContent-Length: %d\r\n\r\n' % (get_date_header(), len(payload))
                  self.headers_sent = True
                  data += payload
                  self.request.write(data)
              else:
                  data = 'HTTP/1.1 200 Ok\r\nDate: %s\r\nContent-Length: 0\r\n\r\n' % get_date_header()
                  self.headers_sent = True
                  self.request.write(data)
  
          # if we are in chunked mode, send end chunk with length 0
          elif self.sent_chunks:
              logger.debug("End chunk")
              self.request.write("0\r\n")
              #we could now send some more headers resp. trailers
              self.request.write("\r\n")
  
          self.request.finish()
docs: Detect if process has closed pipes and we can finish