Skip to content

Commit

Permalink
protect appendFile calls with mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Nov 10, 2023
1 parent 5097e58 commit d817598
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/wrangler/src/utils/debug-log-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { appendFile } from "node:fs/promises";
import { getEnvironmentVariableFactory } from "../environment-variables/factory";
import { getBasePath } from "../paths";
import { logger } from "../logger";
import { Mutex } from "miniflare";

const getDebugFilepath = getEnvironmentVariableFactory({
variableName: "WRANGLER_DEBUG_LOG",
Expand All @@ -26,22 +27,25 @@ const getDebugFilepath = getEnvironmentVariableFactory({
});

const debugLogFilepath = getDebugFilepath();
const mutex = new Mutex();

let hasLoggedLocation = false;
let hasLoggedError = false;

export function appendToDebugLogFile(...args: unknown[]) {
export async function appendToDebugLogFile(...args: unknown[]) {
const entry = `
--- ${new Date().toISOString()}
${format(...args)}
---
`;

appendFile(debugLogFilepath, entry).catch((err) => {
if (hasLoggedError) return;
hasLoggedError = true;
logger.error(`Failed to write to debug log file`, err);
});
await mutex.runWith(() =>
appendFile(debugLogFilepath, entry).catch((err) => {
if (hasLoggedError) return;
hasLoggedError = true;
logger.error(`Failed to write to debug log file`, err);
})
);

if (hasLoggedLocation) return;
hasLoggedLocation = true;
Expand Down

0 comments on commit d817598

Please sign in to comment.