Skip to content

Does Deno.writeTextFile acquire a lock? #26692

Answered by 0f-0b
alexgleason asked this question in Q&A
Discussion options

You must be logged in to vote

It does not, which you can confirm by calling writeTextFile while the file is exclusively locked.

By the way, you forgot to truncate the file after locking it. In case lock contention turns out to be a problem, another way to rewrite a file atomically is to write all data into a temporary file and then replace the original file using a rename.

const tempFile = await Deno.makeTempFile({ dir: dirname(path) });
try {
  await Deno.writeTextFile(tempFile, data);
  await Deno.rename(tempFile, path);
} catch (e) {
  await Deno.remove(tempFile);
  throw e;
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bartlomieju
Comment options

Answer selected by bartlomieju
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants