xfs_scrub_all: fix deadlock if lsblk produces a lot of output
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.
--- scrub/xfs_scrub_all.py.in | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
scrub/xfs_scrub_all.py.in +7 -6
@@ -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)
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.0Fixes f1dca11cad1308 ("xfs_scrub: create a script to scrub all xfs filesystems")Signed-off-by "Darrick J. Wong" <[email protected]>
Looks good to me
Signature · 1 line
- Andrey
Looks good: