Skip to content

Commit 25f5e2f

Browse files
committed
Avoid log read file race
1 parent 37b510d commit 25f5e2f

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

packages/cli/src/cli/commands/local-workflow.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,25 +420,31 @@ async function readLocalRunLogs(
420420
record: LocalWorkflowRunRecord;
421421
}> {
422422
const record = await refreshRunRecord(await readRunRecord(deps.cwd(), runId), deps);
423-
const stat = await fsp.stat(record.logPath).catch((error: unknown) => {
423+
const handle = await fsp.open(record.logPath, 'r').catch((error: unknown) => {
424424
const err = error as NodeJS.ErrnoException;
425425
if (err.code === 'ENOENT') {
426-
return { size: 0 };
426+
return null;
427427
}
428428
throw error;
429429
});
430-
const totalSize = stat.size;
431-
const offset = Math.min(options.offset, totalSize);
432-
const length = Math.max(0, totalSize - offset);
433-
const handle = await fsp.open(record.logPath, 'r').catch((error: unknown) => {
434-
const err = error as NodeJS.ErrnoException;
435-
if (err.code === 'ENOENT') return null;
436-
throw error;
437-
});
438430

439431
let content = '';
440-
if (handle && length > 0) {
432+
let totalSize = 0;
433+
if (handle) {
441434
try {
435+
const stat = await handle.stat();
436+
totalSize = stat.size;
437+
const offset = Math.min(options.offset, totalSize);
438+
const length = Math.max(0, totalSize - offset);
439+
if (length === 0) {
440+
return {
441+
content,
442+
offset: totalSize,
443+
totalSize,
444+
done: TERMINAL_STATUSES.has(record.status),
445+
record,
446+
};
447+
}
442448
const buffer = Buffer.alloc(length);
443449
const result = await handle.read(buffer, 0, length, offset);
444450
content = buffer.subarray(0, result.bytesRead).toString('utf-8');

0 commit comments

Comments
 (0)