From 59214ca29732020614a84eb8171034c4c4a486ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 17 Dec 2021 19:06:01 -0500 Subject: [PATCH] fix: #10086, if pidfile is empty delete --- loader.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/loader.js b/loader.js index 8c3e47e806..7149b8b8a4 100644 --- a/loader.js +++ b/loader.js @@ -187,6 +187,7 @@ Loader.stop = function () { killWorkers(); // Clean up the pidfile + console.log('stop called'); if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { fs.unlinkSync(pidFilePath); } @@ -208,12 +209,25 @@ fs.open(pathToConfig, 'r', (err) => { if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { if (file.existsSync(pidFilePath)) { + let pid = 0; try { - const pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); - process.kill(pid, 0); - process.exit(); - } catch (e) { - fs.unlinkSync(pidFilePath); + pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); + if (pid) { + process.kill(pid, 0); + console.info(`Process "${pid}" from pidfile already running, exiting`); + process.exit(); + } else { + console.info(`Invalid pid "${pid}" from pidfile, deleting pidfile`); + fs.unlinkSync(pidFilePath); + } + } catch (err) { + if (err.code === 'ESRCH') { + console.info(`Process "${pid}" from pidfile not found, deleting pidfile`); + fs.unlinkSync(pidFilePath); + } else { + console.error(err.stack); + throw err; + } } }