Patchwork [3,of,5,hgweb-thread-isolation] hgweb: establish class for holding per request context

login
register
mail settings
Submitter Gregory Szorc
Date Sept. 1, 2015, 7:11 p.m.
Message ID <a3b9bcb5eaa7d0fae4d7.1441134669@gps-mbp>
Download mbox | patch
Permalink /patch/10353/
State Accepted
Headers show

Comments

Gregory Szorc - Sept. 1, 2015, 7:11 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1440280776 25200
#      Sat Aug 22 14:59:36 2015 -0700
# Node ID a3b9bcb5eaa7d0fae4d751c223cfd5b321f4bb8f
# Parent  0a6b4dfb900e860684b8cc2df2788ad78e7f173a
hgweb: establish class for holding per request context

Currently, hgweb applications have many instance variables holding
mutated state. This is somewhat problematic because multiple threads
may race accessing or changing this state.

This patch starts a series that will add more thread safety to
hgweb applications. It will do this by moving mutated state out
of hgweb and into per-request instances of the newly established
"requestcontext" class.

Our new class currently behaves like a proxy to hgweb instances. This
should change once all state is captured in it instead of hgweb. The
effectiveness of this proxy is demonstrated by passing instances of
it - not hgweb instances/self - to various functions.

Patch

diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py
+++ b/mercurial/hgweb/hgweb_mod.py
@@ -60,8 +60,27 @@  def makebreadcrumb(url, prefix=''):
         urlel = os.path.dirname(urlel)
     return reversed(breadcrumb)
 
 
+class requestcontext(object):
+    """Holds state/context for an individual request.
+
+    Servers can be multi-threaded. Holding state on the WSGI application
+    is prone to race conditions. Instances of this class exist to hold
+    mutable and race-free state for requests.
+    """
+    def __init__(self, app):
+        object.__setattr__(self, 'app', app)
+        object.__setattr__(self, 'repo', app.repo)
+
+    # Proxy unknown reads and writes to the application instance
+    # until everything is moved to us.
+    def __getattr__(self, name):
+        return getattr(self.app, name)
+
+    def __setattr__(self, name, value):
+        return setattr(self.app, name, value)
+
 class hgweb(object):
     """HTTP server for individual repositories.
 
     Instances of this class serve HTTP responses for a particular
@@ -192,8 +211,9 @@  class hgweb(object):
         This is typically only called by Mercurial. External consumers
         should be using instances of this class as the WSGI application.
         """
         self.refresh(req)
+        rctx = requestcontext(self)
 
         # work with CGI variables to create coherent structure
         # use SCRIPT_NAME, PATH_INFO and QUERY_STRING as well as our REPO_NAME
 
@@ -222,9 +242,9 @@  class hgweb(object):
             try:
                 if query:
                     raise ErrorResponse(HTTP_NOT_FOUND)
                 if cmd in perms:
-                    self.check_perm(req, perms[cmd])
+                    self.check_perm(rctx, req, perms[cmd])
                 return protocol.call(self.repo, req, cmd)
             except ErrorResponse as inst:
                 # A client that sends unbundle without 100-continue will
                 # break if we respond early.
@@ -283,9 +303,9 @@  class hgweb(object):
             ctype = templater.stringify(ctype)
 
             # check read permissions non-static content
             if cmd != 'static':
-                self.check_perm(req, None)
+                self.check_perm(rctx, req, None)
 
             if cmd == '':
                 req.form['cmd'] = [tmpl.cache['default']]
                 cmd = req.form['cmd'][0]
@@ -296,11 +316,11 @@  class hgweb(object):
                 msg = 'no such method: %s' % cmd
                 raise ErrorResponse(HTTP_BAD_REQUEST, msg)
             elif cmd == 'file' and 'raw' in req.form.get('style', []):
                 self.ctype = ctype
-                content = webcommands.rawfile(self, req, tmpl)
+                content = webcommands.rawfile(rctx, req, tmpl)
             else:
-                content = getattr(webcommands, cmd)(self, req, tmpl)
+                content = getattr(webcommands, cmd)(rctx, req, tmpl)
                 req.respond(HTTP_OK, ctype)
 
             return content
 
@@ -441,7 +461,7 @@  class hgweb(object):
         'gz': ('application/x-gzip', 'tgz', '.tar.gz', None),
         'zip': ('application/zip', 'zip', '.zip', None),
         }
 
-    def check_perm(self, req, op):
+    def check_perm(self, rctx, req, op):
         for permhook in permhooks:
-            permhook(self, req, op)
+            permhook(rctx, req, op)