code: def flush_on_close(self, stream):
          
          assert get_thread_ident() == self.ioloop_thread_id
          # Prevent futher writes
          stream.KATCPServer_closing = True
          # Write empty message to get future that resolves when buffer is flushed
          return stream.write('\n')
docs: Flush tornado iostream write buffer and prevent further writes.
  
          Returns a future that resolves when the stream is flushed.