From 2ca323006864c9a1e8694c6e8c64af3178204179 Mon Sep 17 00:00:00 2001 From: Amit Prinz Setter Date: Thu, 5 Jun 2025 10:07:57 -0700 Subject: [PATCH] persistent logger - check fd before write in append() (dfs 2624) Signed-off-by: Amit Prinz Setter --- src/util/persistent_logger.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/util/persistent_logger.js b/src/util/persistent_logger.js index 0e55bc745d..461742216b 100644 --- a/src/util/persistent_logger.js +++ b/src/util/persistent_logger.js @@ -8,6 +8,7 @@ const P = require('./promise'); const semaphore = require('./semaphore'); const { NewlineReader } = require('./file_reader'); const dbg = require('./debug_module')(__filename); +const APPEND_ATTEMPTS_LIMIT = 5; /** * PersistentLogger is a logger that is used to record data onto disk separated by newlines. @@ -105,10 +106,20 @@ class PersistentLogger { * @param {string} data */ async append(data) { - const fh = await this.init(); - const buf = Buffer.from(data + '\n', 'utf8'); - await fh.write(this.fs_context, buf, buf.length); + + for (let attempt = 0; attempt < APPEND_ATTEMPTS_LIMIT; ++attempt) { + const fh = await this.init(); + //if another process has deleted the active file, + //this process' _poll_active_file_change might have closed the fd + //in that case fd is -1 + //in order to avoid inter-process locking, we just re-init + //the fd to the new active file. + if (fh.fd === -1) continue; + await fh.write(this.fs_context, buf, buf.length); + break; + } + this.local_size += buf.length; }