Skip to content

mkdir cache race condition #8

Description

@mjac

Hi KODUS,

Thank you for your library. I had a failure last night on mkdir($path).

ErrorException Warning: mkdir(): File exists mechanism generic handled false

This occurred in … /vendor/kodus/file-cache/src/FileCache.php in Kodus\Cache\FileCache::mkdir at line 380

        if (!file_exists($parent_path)) {
            $this->mkdir($parent_path); // recursively create parent dirs first
        }
        mkdir($path);
        chmod($path, $this->dir_mode);
    }
}

By inspection, it takes time between file_exists($cache_path) and mkdir($path), especially if $parent_path is missing. If user A and user B are both racing to create this directory then mkdir can be called more than once. In this scenario this will raise an ErrorException warning Warning: mkdir(): File exists

if (! file_exists($cache_path) && file_exists(dirname($cache_path))) {
    $this->mkdir($cache_path); // ensure that the parent path exists
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions