From 2987e92ab786fb9aee715e95ea2ffcda816bfaf4 Mon Sep 17 00:00:00 2001 From: PalmerAL Date: Wed, 13 Nov 2024 13:37:00 -0600 Subject: [PATCH] use atomic writes for session restore --- js/sessionRestore.js | 11 ++++++++--- package.json | 3 ++- scripts/buildBrowser.js | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/js/sessionRestore.js b/js/sessionRestore.js index 63b3861f0..4baef806b 100644 --- a/js/sessionRestore.js +++ b/js/sessionRestore.js @@ -4,6 +4,8 @@ var tabEditor = require('navbar/tabEditor.js') var tabState = require('tabState.js') var settings = require('util/settings/settings.js') var taskOverlay = require('taskOverlay/taskOverlay.js') +const writeFileAtomic = require('write-file-atomic') +const statistics = require('js/statistics.js') const sessionRestore = { savePath: window.globalArgs['user-data-path'] + (platformType === 'windows' ? '\\sessionRestore.json' : '/sessionRestore.json'), @@ -40,11 +42,12 @@ const sessionRestore = { if (forceSave === true || stateString !== sessionRestore.previousState) { if (sync === true) { - fs.writeFileSync(sessionRestore.savePath, JSON.stringify(data)) + writeFileAtomic.sync(sessionRestore.savePath, JSON.stringify(data), {}) } else { - fs.writeFile(sessionRestore.savePath, JSON.stringify(data), function (err) { + writeFileAtomic(sessionRestore.savePath, JSON.stringify(data), {}, function (err) { if (err) { console.warn(err) + statistics.incrementValue('sessionRestoreSaveAsyncWriteErrors') } }) } @@ -171,7 +174,7 @@ const sessionRestore = { var backupSavePath = require('path').join(window.globalArgs['user-data-path'], 'sessionRestoreBackup-' + Date.now() + '.json') - fs.writeFileSync(backupSavePath, savedStringData) + writeFileAtomic.sync(backupSavePath, savedStringData, {}) // destroy any tabs that were created during the restore attempt tabState.initialize() @@ -184,6 +187,8 @@ const sessionRestore = { browserUI.switchToTask(newTask) browserUI.switchToTab(newSessionErrorTab) + + statistics.incrementValue('sessionRestorationErrors') } }, syncWithWindow: function () { diff --git a/package.json b/package.json index a12d06410..c87485daf 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "quick-score": "^0.2.0", "regedit": "^3.0.3", "sortablejs": "^1.15.1", - "stemmer": "^1.0.5" + "stemmer": "^1.0.5", + "write-file-atomic": "^6.0.0" }, "devDependencies": { "archiver": "^4.0.1", diff --git a/scripts/buildBrowser.js b/scripts/buildBrowser.js index db618b408..56d40b379 100644 --- a/scripts/buildBrowser.js +++ b/scripts/buildBrowser.js @@ -34,6 +34,7 @@ function buildBrowser () { }) instance.exclude('chokidar') + instance.exclude('write-file-atomic') instance.transform(renderify) const stream = fs.createWriteStream(outFile, { encoding: 'utf-8' })