diff --git a/trac/web/main.py b/trac/web/main.py
--- a/trac/web/main.py
+++ b/trac/web/main.py
@@ -46,7 +46,7 @@
 from trac.util.text import exception_to_unicode, shorten_line, to_unicode
 from trac.util.translation import _
 from trac.web.api import *
-from trac.web.chrome import Chrome
+from trac.web.chrome import Chrome, add_warning
 from trac.web.clearsilver import HDFWrapper
 from trac.web.href import Href
 from trac.web.session import Session
@@ -214,6 +214,14 @@
                     else: # Genshi
                         template, data, content_type = \
                                   self._post_process_request(req, *resp)
+                        try:
+                            # Give the session a chance to persist changes
+                            req.session.save()
+                        except Exception, e: # TracDatabaseError (#6348)
+                            add_warning(
+                                req, _("Changes to user session couldn't be "
+                                       "saved (%(err)s)",
+                                       err=exception_to_unicode(e)))
                         if 'hdfdump' in req.args:
                             req.perm.require('TRAC_ADMIN')
                             # debugging helper - no need to render first
@@ -224,8 +232,6 @@
                         else:
                             output = chrome.render_template(req, template,
                                                             data, content_type)
-                            # Give the session a chance to persist changes
-                            req.session.save()
                             req.send(output, content_type or 'text/html')
                 else:
                     self._post_process_request(req)
diff --git a/trac/web/session.py b/trac/web/session.py
--- a/trac/web/session.py
+++ b/trac/web/session.py
@@ -42,6 +42,9 @@
         else:
             self.authenticated = False
 
+    def __setitem__(self, key, value):
+        dict.__setitem__(self, key, unicode(value))
+
     def get_session(self, sid, authenticated=False):
         self.env.log.debug('Retrieving session for ID %r', sid)
 

