Skip to content

Commit 2ca3230

Browse files
committed
persistent logger - check fd before write in append() (dfs 2624)
Signed-off-by: Amit Prinz Setter <alphaprinz@gmail.com>
1 parent a68f6ef commit 2ca3230

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/util/persistent_logger.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const P = require('./promise');
88
const semaphore = require('./semaphore');
99
const { NewlineReader } = require('./file_reader');
1010
const dbg = require('./debug_module')(__filename);
11+
const APPEND_ATTEMPTS_LIMIT = 5;
1112

1213
/**
1314
* PersistentLogger is a logger that is used to record data onto disk separated by newlines.
@@ -105,10 +106,20 @@ class PersistentLogger {
105106
* @param {string} data
106107
*/
107108
async append(data) {
108-
const fh = await this.init();
109-
110109
const buf = Buffer.from(data + '\n', 'utf8');
111-
await fh.write(this.fs_context, buf, buf.length);
110+
111+
for (let attempt = 0; attempt < APPEND_ATTEMPTS_LIMIT; ++attempt) {
112+
const fh = await this.init();
113+
//if another process has deleted the active file,
114+
//this process' _poll_active_file_change might have closed the fd
115+
//in that case fd is -1
116+
//in order to avoid inter-process locking, we just re-init
117+
//the fd to the new active file.
118+
if (fh.fd === -1) continue;
119+
await fh.write(this.fs_context, buf, buf.length);
120+
break;
121+
}
122+
112123
this.local_size += buf.length;
113124
}
114125

0 commit comments

Comments
 (0)