From a8e197148381f545ecd8fecac4e9ce94113959d2 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 11 Mar 2021 13:51:29 +0100 Subject: [PATCH] Fix data loss on bad name passed --dev-plugin If data from the database is not loaded but a shutdown is triggered then the master server will overwrite the database with empty data, causing unexpected data loss. This occurs for example when the --dev-plugin option throws. Fix by setting the database entries to null before they are loaded so that should an attempt be made to save them without having been loaded first it will throw an error instead. --- packages/master/master.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/master/master.js b/packages/master/master.js index 6b80e181..27ac8869 100755 --- a/packages/master/master.js +++ b/packages/master/master.js @@ -41,8 +41,10 @@ let loadedPlugins = {}; let devMiddleware; let clusterLogger; let db = { - instances: new Map(), - slaves: new Map(), + instances: null, + slaves: null, + roles: null, + users: null, }; let slaveConnections = new Map(); let controlConnections = []; @@ -489,6 +491,14 @@ async function shutdown() { logger.info("Saving configs"); await fs.outputFile(masterConfigPath, JSON.stringify(masterConfig.serialize(), null, 4)); + if (devMiddleware) { + await new Promise((resolve, reject) => { devMiddleware.close(resolve); }); + } + + if (!db.slaves) { + return; + } + await saveMap(masterConfig.get("master.database_directory"), "slaves.json", db.slaves); await saveInstances(masterConfig.get("master.database_directory"), "instances.json", db.instances); await saveUsers(masterConfig.get("master.database_directory"), "users.json"); @@ -497,10 +507,6 @@ async function shutdown() { stopAcceptingNewSessions = true; - if (devMiddleware) { - await new Promise((resolve, reject) => { devMiddleware.close(resolve); }); - } - let disconnectTasks = []; for (let controlConnection of controlConnections) { controlConnection.connector.setTimeout(masterConfig.get("master.connector_shutdown_timeout"));