procutil: make stdout line-buffered on Windows if connected to TTY
Windows doesn’t support line buffering. Previously, we worked around that by
setting the stream unbuffered. Instead, we can use our own line buffering we
already use on Python 3.
procutil: split import and reassignment
Besides making the code clearer, it will reduce the diff in the next patch.
procutil: move assignments closer to reassignments
Doing reassignments is an anti-pattern IMHO, but I see how it makes sense here.
When first looking at this code after jumping here with ctags, I missed the
fact that stdout was reassigned. To make the code clearer, the assignments
should be as close as possible to the reassignments.
procutil: factor out conditional creation of LineBufferedWrapper
At the same time, document the logic and generalize it to work on all Python
versions.
procutil: define LineBufferedWrapper on all Python versions
There’s nothing Python 3-only about LineBufferedWrapper. In the future, we may
want to use it on Windows, to work around missing line-buffering support.
cleanup: use slightly more meaningful name for temporary variable
Not that it makes a big difference, but using `p` instead of `x` is clearer to
me.
cleanup: use any() instead of checking truthiness of temporary list
It was not immediately obvious to me, when first seeing this, why a list was
created. It needed a second look to understand that the purpose was to check
whether the condition is true for any of the parents. Using any() for that is
clearer.
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.
py3: fix crash when server address is 0.0.0.0 (
issue6362)
`socket.getfqdn()` assumes that the name is passed as `str` on Python 3 and
always returns `str` in this case. Mercurial passed `bytes` (but still expected
a `str` result), which worked by chance in many cases, except for e.g.
b'0.0.0.0', which was returned unchanged, breaking later code.
Instead of calling `socket.getfqdn()`, we can also use `self.server_name` from
the base `HTTPServer` class, which already stores the FQDN of the locally-bound
socket name (see `BaseHTTPServer.py` in the Python 2 stdlib and
`http/server.py` in the Python 3 stdlib).