From a963878af57162bbc7bbd6b54ad455ce76a02003 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Wed, 10 Aug 2022 17:32:36 +0200 Subject: [PATCH] fix(fs/Local#lock): never fail on release Related to zammad#8826 Also, log properly if the log is compromised. --- @xen-orchestra/fs/src/local.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/@xen-orchestra/fs/src/local.js b/@xen-orchestra/fs/src/local.js index 006bbc35e..454e37c6a 100644 --- a/@xen-orchestra/fs/src/local.js +++ b/@xen-orchestra/fs/src/local.js @@ -2,10 +2,13 @@ import df from '@sindresorhus/df' import fs from 'fs-extra' import identity from 'lodash/identity.js' import lockfile from 'proper-lockfile' +import { createLogger } from '@xen-orchestra/log' import { fromEvent, retry } from 'promise-toolbox' import RemoteHandlerAbstract from './abstract' +const { warn } = createLogger('xo:fs:local') + // save current stack trace and add it to any rejected error // // This is especially useful when the resolution is separate from the initial @@ -106,8 +109,19 @@ export default class LocalHandler extends RemoteHandlerAbstract { return this._addSyncStackTrace(fs.readdir(this._getFilePath(dir))) } - _lock(path) { - return lockfile.lock(this._getFilePath(path)) + async _lock(path) { + const release = lockfile.lock(this._getFilePath(path), { + onCompromised: async error => { + warn('lock compromised', { error }) + }, + }) + return async () => { + try { + await release() + } catch (error) { + warn('lock could not be released', { error }) + } + } } _mkdir(dir, { mode }) {