From cfff79225cb45d384bf7cb5036c4cb3a59c8b0ab Mon Sep 17 00:00:00 2001 From: Arthur Cinader <700572+acinader@users.noreply.github.com> Date: Sun, 11 Mar 2018 12:31:03 -0700 Subject: [PATCH 1/2] Tell the process to exit once our shutdown hook has run. fixes: 4593 I think that this is proper hygiene Wait a tick before process exit, not for any particular reason than it seems defensive? --- src/ParseServer.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ParseServer.js b/src/ParseServer.js index 76631ea3d9..e4e346b4af 100644 --- a/src/ParseServer.js +++ b/src/ParseServer.js @@ -338,7 +338,11 @@ function configureListeners(parseServer) { destroyAliveConnections(); server.close(); parseServer.handleShutdown(); + setImmediate(() => { // give anything that might need it a tick + process.exit(0) + }); }; + process.on('SIGTERM', handleShutdown); process.on('SIGINT', handleShutdown); } From c82580286a38ca4057be39cb072c649fef5e92e8 Mon Sep 17 00:00:00 2001 From: Arthur Cinader <700572+acinader@users.noreply.github.com> Date: Fri, 16 Mar 2018 14:47:34 -0700 Subject: [PATCH 2/2] rip out the special handeling that is no longer needed because https://github.com/nodejs/node/issues/2642 has been fixed so server will just do the right thing now. --- src/ParseServer.js | 42 +----------------------------------------- 1 file changed, 1 insertion(+), 41 deletions(-) diff --git a/src/ParseServer.js b/src/ParseServer.js index e4e346b4af..12e2acde0d 100644 --- a/src/ParseServer.js +++ b/src/ParseServer.js @@ -231,10 +231,7 @@ class ParseServer { if (options.startLiveQueryServer || options.liveQueryServerOptions) { this.liveQueryServer = ParseServer.createLiveQueryServer(server, options.liveQueryServerOptions); } - /* istanbul ignore next */ - if (!process.env.TESTING) { - configureListeners(this); - } + this.expressApp = app; return this; } @@ -310,41 +307,4 @@ function injectDefaults(options: ParseServerOptions) { ))); } -// Those can't be tested as it requires a subprocess -/* istanbul ignore next */ -function configureListeners(parseServer) { - const server = parseServer.server; - const sockets = {}; - /* Currently, express doesn't shut down immediately after receiving SIGINT/SIGTERM if it has client connections that haven't timed out. (This is a known issue with node - https://github.com/nodejs/node/issues/2642) - This function, along with `destroyAliveConnections()`, intend to fix this behavior such that parse server will close all open connections and initiate the shutdown process as soon as it receives a SIGINT/SIGTERM signal. */ - server.on('connection', (socket) => { - const socketId = socket.remoteAddress + ':' + socket.remotePort; - sockets[socketId] = socket; - socket.on('close', () => { - delete sockets[socketId]; - }); - }); - - const destroyAliveConnections = function() { - for (const socketId in sockets) { - try { - sockets[socketId].destroy(); - } catch (e) { /* */ } - } - } - - const handleShutdown = function() { - process.stdout.write('Termination signal received. Shutting down.'); - destroyAliveConnections(); - server.close(); - parseServer.handleShutdown(); - setImmediate(() => { // give anything that might need it a tick - process.exit(0) - }); - }; - - process.on('SIGTERM', handleShutdown); - process.on('SIGINT', handleShutdown); -} - export default ParseServer;