# HG changeset patch # User Pulkit Goyal <7895pulkit@gmail.com> # Date 2020-07-03 08:15:59 # Node ID 6118408bc3cc13c741e94b42cc665d90c478833b # Parent 36ea1442b7bc624245607dcfe52d9a87f6d0a827 chg: suppress OSError in _restoreio() and add some logging (issue6330) According to issue6330, running chg on heavy loaded systems can lead to following error: ``` Traceback (most recent call last): File "path-to-hg/mercurial/commandserver.py", line 650, in _acceptnewconnection self._runworker(conn) File "path-to-hg/mercurial/commandserver.py", line 701, in _runworker prereposetups=[self._reposetup], File "path-to-hg/mercurial/commandserver.py", line 470, in _serverequest sv.cleanup() File "path-to-hg/mercurial/chgserver.py", line 381, in cleanup self._restoreio() File "path-to-hg/mercurial/chgserver.py", line 444, in _restoreio os.dup2(fd, fp.fileno()) OSError: [Errno 16] Device or resource busy ``` [man dup2] indicates that, on Linux, EBUSY comes from a race condition between open() and dup2(). However it's not clear why open() race occurred for newfd=stdin/out/err. We suppress the OSError in _restoreio() since the forked worker process will finish anyway and add some logging. Thanks to Mitchell Plamann for a detailed bug description and Yuya Nishihara for suggesting the fix. diff --git a/mercurial/chgserver.py b/mercurial/chgserver.py --- a/mercurial/chgserver.py +++ b/mercurial/chgserver.py @@ -442,7 +442,20 @@ class chgcmdserver(commandserver.server) if newfp is not fp: newfp.close() # restore original fd: fp is open again - os.dup2(fd, fp.fileno()) + try: + os.dup2(fd, fp.fileno()) + except OSError as err: + # According to issue6330, running chg on heavy loaded systems + # can lead to EBUSY. [man dup2] indicates that, on Linux, + # EBUSY comes from a race condition between open() and dup2(). + # However it's not clear why open() race occurred for + # newfd=stdin/out/err. + self.ui.log( + b'chgserver', + b'got %s while duplicating %s\n', + stringutil.forcebytestr(err), + fn, + ) os.close(fd) setattr(self, cn, ch) setattr(ui, fn, fp)