From 3d4acae311d70c7a4a9129412f12212428084fff Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Thu, 10 Aug 2023 01:14:10 +0200 Subject: [PATCH] Make SVR.JS gracefully exit on "stop" command. --- config.json | 2 +- index.html | 2 +- svr.js | 71 ++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/config.json b/config.json index 4dbd48a..7cc7bd0 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,7 @@ "port": 80, "pubport": 80, "page404": "404.html", - "timestamp": 1691613309162, + "timestamp": 1691622823922, "blacklist": [], "nonStandardCodes": [], "enableCompression": true, diff --git a/index.html b/index.html index e3a91b0..5a0a51f 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@ -

Welcome to SVR.JS Nightly-GitMaster

+

Welcome to SVR.JS Nightly-GitMain

WARNING!
This version is only for test purposes and may be unstable. diff --git a/svr.js b/svr.js index eb90ea8..9bb0e4c 100644 --- a/svr.js +++ b/svr.js @@ -4772,9 +4772,13 @@ function start(init) { var commands = { close: function () { try { - server.close(); - if (secure && !disableNonEncryptedServer) { - server2.close(); + if(server.listening || server2.listening) { + server.close(); + if (secure && !disableNonEncryptedServer) { + server2.close(); + } + } else { + throw new Error("Server is already closed."); } if (cluster.isPrimary === undefined) serverconsole.climessage("Server closed."); else { @@ -4788,11 +4792,15 @@ function start(init) { }, open: function () { try { - if (secure) { - server.listen(sport); - if (!disableNonEncryptedServer) server2.listen(port); + if(!server.listening) { + if (secure) { + server.listen(sport); + if (!disableNonEncryptedServer) server2.listen(port); + } else { + server.listen(port); // Reopen Server + } } else { - server.listen(port); // Reopen Server + throw new Error("Server is already opened."); } if (cluster.isPrimary === undefined) serverconsole.climessage("Server opened."); else { @@ -4821,10 +4829,46 @@ function start(init) { }, stop: function (retcode) { reallyExiting = true; - if (typeof retcode == "number") { - process.exit(retcode); + if((!cluster.isPrimary && cluster.isPrimary !== undefined) && server.listening) { + try { + server.close(function() { + if(server2.listening) { + try { + server2.close(function() { + if (typeof retcode == "number") { + process.exit(retcode); + } else { + process.exit(0); + } + }); + } catch(err) { + if (typeof retcode == "number") { + process.exit(retcode); + } else { + process.exit(0); + } + } + } else { + if (typeof retcode == "number") { + process.exit(retcode); + } else { + process.exit(0); + } + } + }); + } catch(err) { + if (typeof retcode == "number") { + process.exit(retcode); + } else { + process.exit(0); + } + } } else { - process.exit(0); + if (typeof retcode == "number") { + process.exit(retcode); + } else { + process.exit(0); + } } }, clear: function () { @@ -4915,8 +4959,11 @@ function start(init) { } process.send("\x12END"); } else if (line == "\x14KILLPING") { - process.send("\x12KILLOK"); - process.send("\x12END"); + if(!reallyExiting) { + process.send("\x12KILLOK"); + process.send("\x12END"); + } + // Refuse to send, when it's really exiting. Main process will treat the worker as hung up anyway... } else if (line == "\x14KILLREQ") { if(reqcounter - reqcounterKillReq < 2) { process.send("\x12KILLTERMMSG");