Commits

Robert Brewer committed d73d60d

Applying nogensess.patch from #594.

Comments (0)

Files changed (1)

cherrypy/lib/sessions.py

 
 def save():
     """Save any changed session data."""
-    def wrap_body(body):
-        """Response.body wrapper which saves session data."""
-        if isinstance(body, types.GeneratorType):
-            # If the body is a generator, we have to save the data
-            #   *after* the generator has been consumed
-            for line in body:
-                yield line
-            cherrypy.session.save()
-        else:
-            # If the body is not a generator, we save the data
-            #   before the body is returned (so we can release the lock).
-            cherrypy.session.save()
-            for line in body:
-                yield line
-    cherrypy.response.body = wrap_body(cherrypy.response.body)
+    # Guard against running twice
+    if hasattr(cherrypy.request, "_sessionsaved"):
+        return
+    cherrypy.request._sessionsaved = True
+    
+    if cherrypy.response.stream:
+        # If the body is being streamed, we have to save the data
+        #   *after* the response has been written out
+        cherrypy.request.hooks.attach('on_end_request', cherrypy.session.save)
+    else:
+        # If the body is not being streamed, we save the data now
+        # (so we can release the lock).
+        if isinstance(cherrypy.response.body, types.GeneratorType):
+            cherrypy.response.collapse_body()
+        cherrypy.session.save()
 save.failsafe = True
 
 def close():
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.