memory: Fix memory leak.

This fix is based on
https://github.com/tornadoweb/tornado/commit/769bc52e11656788782a6e7a922ef646503f9ab0

Change-Id: I22ca6ad29cfbf642c85a8ff0393dee94c818930b
diff --git a/tornado/httpserver.py b/tornado/httpserver.py
index 75c86b4..0c5bd00 100644
--- a/tornado/httpserver.py
+++ b/tornado/httpserver.py
@@ -170,6 +170,18 @@
         self._header_callback = stack_context.wrap(self._on_headers)
         self.stream.read_until(b("\r\n\r\n"), self._header_callback)
         self._write_callback = None
+        self._close_callback = None
+
+    def set_close_callback(self, callback):
+        self._close_callback = stack_context.wrap(callback)
+        self.stream.set_close_callback(self._on_connection_close)
+
+    def _on_connection_close(self):
+        callback = self._close_callback
+        self._close_callback = None
+        callback()
+        # Delete any unfinished callbacks to break up reference cycles.
+        self._write_callback = None
 
     def write(self, chunk, callback=None):
         """Writes a chunk of output to the stream."""
diff --git a/tornado/iostream.py b/tornado/iostream.py
index 6db0fdf..7ce75db 100644
--- a/tornado/iostream.py
+++ b/tornado/iostream.py
@@ -242,6 +242,8 @@
             cb = self._close_callback
             self._close_callback = None
             self._run_callback(cb)
+            # Delete any unfinished callbacks to break up reference cycles.
+            self._read_callback = self._write_callback = None
 
     def reading(self):
         """Returns true if we are currently reading from the stream."""
diff --git a/tornado/web.py b/tornado/web.py
index a7c9637..10e3420 100644
--- a/tornado/web.py
+++ b/tornado/web.py
@@ -124,7 +124,7 @@
         self.clear()
         # Check since connection is not available in WSGI
         if getattr(self.request, "connection", None):
-            self.request.connection.stream.set_close_callback(
+            self.request.connection.set_close_callback(
                 self.on_connection_close)
         self.initialize(**kwargs)