# HG changeset patch # User Gregory Szorc # Date 1442085399 25200 # Node ID 7df5d476087392e217699a41c11fbe8cd48713b2 # Parent bf81b696b8f4967a4b9b65c842f456cc8a3ca9b4 hgweb: consume generator inside context manager (issue4756) If code inside a context manager returns a generator, the context manager exits before the generator is iterated. hgweb was using a context manager to control thread safe access to a localrepository instance. But it was returning a generator, so there was a race condition between a previous request streaming a response to the client and a new request obtaining the released but in use repository. By iterating the generator inside the context manager, we ensure we don't release the repo instance until after the response has finished. With this change, hgweb finally appears to have full localrepository isolation between threads. I can no longer reproduce the 2 exceptions reported in issue4756. test-hgweb-non-interactive.t has been modified to consume the output of calling into a WSGI application. Without this, execution of the WSGI application stalls because of the added yield statement. diff -r bf81b696b8f4 -r 7df5d4760873 mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py Thu Aug 27 14:46:26 2015 -0400 +++ b/mercurial/hgweb/hgweb_mod.py Sat Sep 12 12:16:39 2015 -0700 @@ -275,7 +275,8 @@ should be using instances of this class as the WSGI application. """ with self._obtainrepo() as repo: - return self._runwsgi(req, repo) + for r in self._runwsgi(req, repo): + yield r def _runwsgi(self, req, repo): rctx = requestcontext(self, repo) diff -r bf81b696b8f4 -r 7df5d4760873 tests/test-hgweb-non-interactive.t --- a/tests/test-hgweb-non-interactive.t Thu Aug 27 14:46:26 2015 -0400 +++ b/tests/test-hgweb-non-interactive.t Sat Sep 12 12:16:39 2015 -0700 @@ -58,7 +58,8 @@ > } > > i = hgweb('.') - > i(env, startrsp) + > for c in i(env, startrsp): + > pass > print '---- ERRORS' > print errors.getvalue() > print '---- OS.ENVIRON wsgi variables'