From fa3ca671959f5dea8b13b58a7ac8ca21a3df3ab1 Mon Sep 17 00:00:00 2001 From: Reinis Muiznieks Date: Tue, 11 Jun 2024 11:12:26 +0300 Subject: [PATCH] Linux/Darwin don't crash on spawn EBADF errors --- lib/filesystem.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 1128fa0a..3f5dc6f0 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -569,13 +569,13 @@ function blkStdoutToObject(stdout) { function blockDevices(callback) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { process.nextTick(() => { let data = []; if (_linux) { // see https://wiki.ubuntuusers.de/lsblk/ // exec("lsblk -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,TRAN,SERIAL,LABEL,MODEL,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,SCHED,RQ-SIZE,RA,WSAME", function (error, stdout) { - exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,TRAN,SERIAL,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) { + const process = exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,TRAN,SERIAL,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) { if (!error) { let lines = blkStdoutToObject(stdout).split('\n'); data = parseBlk(lines); @@ -586,7 +586,7 @@ function blockDevices(callback) { } resolve(data); } else { - exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) { + const process2 = exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) { if (!error) { let lines = blkStdoutToObject(stdout).split('\n'); data = parseBlk(lines); @@ -597,11 +597,19 @@ function blockDevices(callback) { } resolve(data); }); + + process2.on('error', function (err) { + reject(err); + }); } }); + + process.on('error', function (err) { + reject(err); + }); } if (_darwin) { - exec('diskutil info -all', { maxBuffer: 1024 * 1024 }, function (error, stdout) { + const process = exec('diskutil info -all', { maxBuffer: 1024 * 1024 }, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); // parse lines into temp array of devices @@ -613,6 +621,10 @@ function blockDevices(callback) { } resolve(data); }); + + process.on('error', function (err) { + reject(err); + }); } if (_sunos) { if (callback) { callback(data); } @@ -726,7 +738,7 @@ function calcFsSpeed(rx, wx) { function fsStats(callback) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { process.nextTick(() => { if (_windows || _freebsd || _openbsd || _netbsd || _sunos) { return resolve(null); @@ -747,7 +759,7 @@ function fsStats(callback) { if ((_fs_speed && !_fs_speed.ms) || (_fs_speed && _fs_speed.ms && Date.now() - _fs_speed.ms >= 500)) { if (_linux) { // exec("df -k | grep /dev/", function(error, stdout) { - exec('lsblk -r 2>/dev/null | grep /', { maxBuffer: 1024 * 1024 }, function (error, stdout) { + const process = exec('lsblk -r 2>/dev/null | grep /', { maxBuffer: 1024 * 1024 }, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); let fs_filter = []; @@ -759,7 +771,7 @@ function fsStats(callback) { }); let output = fs_filter.join('|'); - exec('cat /proc/diskstats | egrep "' + output + '"', { maxBuffer: 1024 * 1024 }, function (error, stdout) { + const process2 = exec('cat /proc/diskstats | egrep "' + output + '"', { maxBuffer: 1024 * 1024 }, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); lines.forEach(function (line) { @@ -778,6 +790,10 @@ function fsStats(callback) { } resolve(result); }); + + process2.on('error', function (err) { + reject(err); + }); } else { if (callback) { callback(result); @@ -785,9 +801,13 @@ function fsStats(callback) { resolve(result); } }); + + process.on('error', function (err) { + reject(err); + }); } if (_darwin) { - exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', { maxBuffer: 1024 * 1024 }, function (error, stdout) { + const process = exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', { maxBuffer: 1024 * 1024 }, function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); lines.forEach(function (line) { @@ -806,6 +826,10 @@ function fsStats(callback) { } resolve(result); }); + + process.on('error', function (err) { + reject(err); + }); } } else { result.ms = _fs_speed.last_ms;