Skip to content

set method and the cache file handling should be concurrent access safe #3

@balsa02

Description

@balsa02

the set method is not concurrent access safe, because it's possible to read the cache files while they are being rewritten by a cache update request.

try {
      await ensureDirExists(path.dirname(filename));
     // you can read these files from an other green thread at the same time
      await fs.writeFile(filename + ".data", compressedValue);
      await fs.writeFile(filename + ".meta", JSON.stringify(metadata));
 }

replace them with some atomic file system method, like rename:

  try {
      await ensureDirExists(path.dirname(filename));
      const tmpdir = await fs.mkdtemp(path.dirname(filename));
      await fs.writeFile(tmpdir + "/tmp.data", compressedValue);
      await fs.writeFile(tmpdir + "/tmp.meta", JSON.stringify(metadata));
      await fs.rename(tmpdir + "/tmp.data", filename + ".data");
      await fs.rename(tmpdir + "/tmp.meta", filename + ".meta");
      await fs.rmdir(tmpdir);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions