[PATCH 4 of 5] cmdserver: write channel header and payload by a single write() call

Yuya Nishihara yuya at tcha.org
Wed Nov 2 08:02:16 EDT 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1456720914 -32400
#      Mon Feb 29 13:41:54 2016 +0900
# Node ID e1a050ebbf75c6f512fe041864a1e53b647c3967
# Parent  6f7e69bc46fc158f2df39f94f6e314fc7a66eca1
# EXP-Topic stdio
cmdserver: write channel header and payload by a single write() call

This makes a channeledoutput thread-safe as long as the underlying fwrite() is
thread-safe. Both POSIX and Windows implementations are documented as MT-safe.

MT-safety is necessary to use ui.fout and ui.ferr in hgweb.

diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -54,8 +54,8 @@ class channeledoutput(object):
     def write(self, data):
         if not data:
             return
-        self.out.write(struct.pack('>cI', self.channel, len(data)))
-        self.out.write(data)
+        # single write() to guarantee the same atomicity as the underlying file
+        self.out.write(struct.pack('>cI', self.channel, len(data)) + data)
         self.out.flush()
 
     def __getattr__(self, attr):


More information about the Mercurial-devel mailing list