From 1c646463930b48da4f0f9e1470abb22550b36c52 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 19 Mar 2024 10:04:32 +0000 Subject: [PATCH] Fix quit command to work gracefully --- core/modules/commands/quit.js | 5 ++--- core/modules/server/server.js | 5 +++++ plugins/tiddlywiki/multiwikiserver/modules/mws-server.js | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/core/modules/commands/quit.js b/core/modules/commands/quit.js index 5d99985eb..4aad5ad83 100644 --- a/core/modules/commands/quit.js +++ b/core/modules/commands/quit.js @@ -25,9 +25,8 @@ var Command = function(params,commander,callback) { }; Command.prototype.execute = function() { - // The Node.js docs are very clear that exiting in this way can be dangerous because pending I/O is cancelled. - // TODO: stop the server listeners explicitly so that Node.js will exit the process naturally. - process.exit(); + // We don't actually quit, we just issue the "th-quit" hook to give listeners a chance to exit + $tw.hooks.invokeHook("th-quit"); return null; }; diff --git a/core/modules/server/server.js b/core/modules/server/server.js index 258ddfa31..ce40bb3ae 100644 --- a/core/modules/server/server.js +++ b/core/modules/server/server.js @@ -359,6 +359,11 @@ Server.prototype.listen = function(port,host,prefix) { } // Display the port number after we've started listening (the port number might have been specified as zero, in which case we will get an assigned port) server.on("listening",function() { + // Stop listening when we get the "th-quit" hook + $tw.hooks.addHook("th-quit",function() { + server.close(); + }); + // Log listening details var address = server.address(), url = self.protocol + "://" + (address.family === "IPv6" ? "[" + address.address + "]" : address.address) + ":" + address.port + prefix; $tw.utils.log("Serving on " + url,"brown/orange"); diff --git a/plugins/tiddlywiki/multiwikiserver/modules/mws-server.js b/plugins/tiddlywiki/multiwikiserver/modules/mws-server.js index 18cb2893c..ec2bb1b8f 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/mws-server.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/mws-server.js @@ -466,6 +466,11 @@ Server.prototype.listen = function(port,host,prefix,options) { }); // Display the port number after we've started listening (the port number might have been specified as zero, in which case we will get an assigned port) server.on("listening",function() { + // Stop listening when we get the "th-quit" hook + $tw.hooks.addHook("th-quit",function() { + server.close(); + }); + // Log listening details var address = server.address(), url = self.protocol + "://" + (address.family === "IPv6" ? "[" + address.address + "]" : address.address) + ":" + address.port + prefix; $tw.utils.log("Serving on " + url,"brown/orange");