Skip to contentSkip to conversation

xfs_scrub_all: fix deadlock if lsblk produces a lot of output

PATCH In review Darrick J. Wong 3 participants 3 messages Apr 30 – May 7

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs_scrub_all: fix deadlock if lsblk produces a lot of output

From: Darrick J. Wong <[email protected]>

Patrick Fischer reported a deadlock in find_mounts() that is the result of lsblk producing so much output that it fills the pipe buffer. When this happens, lsblk blocks on write()ing the pipe, at which point waiting for lsblk to exit will also block forever.

Now that we can be reasonably assured that everyone has Python 3.5 (because RHEL6 is long dead), we can replace this whole mess with a call to subprocess.run that captures the output. The json library can convert a byte array directly to a python dict, which means we don't need to concatenate iterated lines or any of that stuff anymore.

Reported-by [email protected]
Cc <[email protected]> # v4.15.0
Fixes f1dca11cad1308 ("xfs_scrub: create a script to scrub all xfs filesystems")
---
 scrub/xfs_scrub_all.py.in |   13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
scrub/xfs_scrub_all.py.in +7 -6
index 9f861639a43ce4..dbb6c36e467d3f 100644
@@ -53,15 +53,16 @@ def find_mounts():
 
 	fs = {}
 	cmd=['lsblk', '-o', 'NAME,KNAME,TYPE,FSTYPE,MOUNTPOINT', '-J']
-	result = subprocess.Popen(cmd, stdout=subprocess.PIPE)
-	result.wait()
-	if result.returncode != 0:
+	try:
+		proc = subprocess.run(cmd, capture_output = True, text = True, check = True)
+	except Exception as e:
+		print(e)
+		return fs
+	if proc.returncode != 0:
 		return fs
-	sarray = [x.decode(sys.stdout.encoding) for x in result.stdout.readlines()]
-	output = ' '.join(sarray)
-	bdevdata = json.loads(output)
 
 	# The lsblk output had better be in disks-then-partitions order
+	bdevdata = json.loads(proc.stdout)
 	for bdev in bdevdata['blockdevices']:
 		lastdisk = bdev['kname']
 		find_xfs_mounts(bdev, fs, lastdisk)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Patrick Fischer reported a deadlock in find_mounts() that is the result of lsblk producing so much output that it fills the pipe buffer. When this happens, lsblk blocks on write()ing the pipe, at which point waiting for lsblk to exit will also block forever.

Now that we can be reasonably assured that everyone has Python 3.5 (because RHEL6 is long dead), we can replace this whole mess with a call to subprocess.run that captures the output. The json library can convert a byte array directly to a python dict, which means we don't need to concatenate iterated lines or any of that stuff anymore.

Quoted 5 lines
Reported-by [email protected]
Cc <[email protected]> # v4.15.0
Fixes f1dca11cad1308 ("xfs_scrub: create a script to scrub all xfs filesystems")

Looks good to me

Signature · 1 line
- Andrey