diff -r 15cfd343d3a9 -r 40da2d7b4871 mercurial/commandserver.py --- a/mercurial/commandserver.py Wed Jan 10 20:02:35 2018 -0800 +++ b/mercurial/commandserver.py Sun Jan 14 19:30:48 2018 -0800 @@ -247,13 +247,13 @@ req = dispatch.request(args[:], copiedui, self.repo, self.cin, self.cout, self.cerr) - ret = (dispatch.dispatch(req) or 0) & 255 # might return None - - # restore old cwd - if '--cwd' in args: - os.chdir(self.cwd) - - self.cresult.write(struct.pack('>i', int(ret))) + try: + ret = (dispatch.dispatch(req) or 0) & 255 # might return None + self.cresult.write(struct.pack('>i', int(ret))) + finally: + # restore old cwd + if '--cwd' in args: + os.chdir(self.cwd) def getencoding(self): """ writes the current encoding to the result channel """