Skip to contentSkip to conversation

test-runner: fix duplicate process output

PATCH Superseded James Prestwood 1 participant 2 messages Mar 1, 2021 – Mar 2, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test-runner: fix duplicate process output

Process output was being duplicated when -v was used. This was due to both stderr and stdout being appended to the write_fd list as well as stderr being set to stdout in the Popen call.

To fix this only stdout should be appended to the write_fd list, but then there comes a problem with closing the streams. stdout cannot be closed, so instead it is special cased. A new verbose boolean was added to Process which, if True, will cause any output to be written to stdout explicitly.

---
 tools/test-runner | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/test-runner +9 -3
index eab0f0ab..6e8966f4 100755
@@ -174,6 +174,7 @@ class Process:
 		self.write_fds = []
 		self.io_watch = None
 		self.cleanup = cleanup
+		self.verbose = False
 
 		if not namespace:
 			self.output_name = '/tmp/%s-out' % self.name
@@ -195,8 +196,7 @@ class Process:
 		if ctx:
 			# Verbose requested, add stdout/stderr to write FD list
 			if self.name in ctx.args.verbose:
-				self.write_fds.append(sys.__stdout__)
-				self.write_fds.append(sys.__stderr__)
+				self.verbose = True
 
 			# Add output file to FD list
 			if outfile:
@@ -234,7 +234,7 @@ class Process:
 				self.io_watch = GLib.io_add_watch(self.stdout, GLib.IO_IN,
 								self.io_callback)
 
-		self.pid = subprocess.Popen(self.args, stdout=self.stdout, stderr=subprocess.STDOUT,
+		self.pid = subprocess.Popen(self.args, stdout=self.stdout, stderr=self.stdout,
 						env=env, cwd=os.getcwd())
 
 		print("Starting process {}".format(self.pid.args))
@@ -261,6 +261,9 @@ class Process:
 
 			self.write_fds = []
 
+		if self.verbose:
+			sys.__stdout__.write(self.out)
+
 		print("%s returned %d" % (args[0], self.ret))
 		if check and self.ret != 0:
 			raise subprocess.CalledProcessError(returncode=self.ret, cmd=self.args)
@@ -284,6 +287,9 @@ class Process:
 		for f in self.write_fds:
 			f.write(data)
 
+		if self.verbose:
+			sys.__stdout__.write(self.out)
+
 		return True
 
 	def __del__(self):
Signature · 1 line
2.26.2
#1 lore raw

There seems to be an issue with this patch that Andrew found, please wait for v2.

Thanks,
James

Quoted 71 lines · James Prestwood

Process output was being duplicated when -v was used. This was due to both stderr and stdout being appended to the write_fd list as well as stderr being set to stdout in the Popen call.

To fix this only stdout should be appended to the write_fd list, but then there comes a problem with closing the streams. stdout cannot be closed, so instead it is special cased. A new verbose boolean was added to Process which, if True, will cause any output to be written to stdout explicitly.

---
 tools/test-runner | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/test-runner +1 -0
index eab0f0ab..6e8966f4 100755
@@ -174,6 +174,7 @@ class Process:
 		self.write_fds = []
 		self.io_watch = None
 		self.cleanup = cleanup
+		self.verbose = False
 
 		if not namespace:
 			self.output_name = '/tmp/%s-out' % self.name
@@ -195,8 +196,7 @@ class Process:
 		if ctx:
 			# Verbose requested, add stdout/stderr to write

FD list

tools/test-runner +2 -3
 			if self.name in ctx.args.verbose:
-				self.write_fds.append(sys.__stdout__)
-				self.write_fds.append(sys.__stderr__)
+				self.verbose = True
 
 			# Add output file to FD list
 			if outfile:
@@ -234,7 +234,7 @@ class Process:
 				self.io_watch =
 GLib.io_add_watch(self.stdout, GLib.IO_IN,
 								self.io
 _callback)
 
-		self.pid = subprocess.Popen(self.args,
 stdout=self.stdout, stderr=subprocess.STDOUT,
+		self.pid = subprocess.Popen(self.args,

stdout=self.stdout, stderr=self.stdout,
env=env,
cwd=os.getcwd())

tools/test-runner +3 -0
 		print("Starting process {}".format(self.pid.args))
@@ -261,6 +261,9 @@ class Process:
 
 			self.write_fds = []
 
+		if self.verbose:
+			sys.__stdout__.write(self.out)
+
 		print("%s returned %d" % (args[0], self.ret))
 		if check and self.ret != 0:
 			raise

subprocess.CalledProcessError(returncode=self.ret, cmd=self.args)

tools/test-runner +3 -0
@@ -284,6 +287,9 @@ class Process:
 		for f in self.write_fds:
 			f.write(data)
 
+		if self.verbose:
+			sys.__stdout__.write(self.out)
+
 		return True
 
 	def __del__(self):