Questions? Discussions: https://phalcon.io/discussions or Discord: https://phalcon.io/discord
Describe the bug
I have an interesting stream cache warning that shows up randomly. The error appears in the error log as a mkdir() "file exists" and reports the cache set() line in my application.
$cache->set($cacheKey, $data, 604800);
According to ChatGPT, the issue appears to be a race condition in the Phalcon stream handler where two calls are made at the same time to reset the cache. Specifically, the issue was identified with this mkdir() call:
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
The likely sequence is:
Request A: directory absent
Request B: directory absent
Request A: mkdir() succeeds
Request B: mkdir() warns "File exists"
BubbaG (my human readable name for the stupid ChatGPT name) suggested something like this could address the issue, where the condition of the directory already existing is not treated as a failure:
if (!is_dir($directory) && !@mkdir($directory, 0777, true)) {
clearstatcache(true, $directory);
if (!is_dir($directory)) {
$error = error_get_last();
throw new RuntimeException($error['message'] ?? "Unable to create cache directory [{$directory}]");
}
}
To Reproduce
I have no idea how you would create the timing necessary to test this. You would need the requests to fire at the same time.
Expected behavior
I would expect no mkdir() error logging when the directory already exists due to the first operation creating it.
Details
- Phalcon version: 5.20.3
- PHP Version: 8.4
- Operating System: Ubuntu 24.04.4
- Installation type: Compiling from source
- Zephir version (if any):
- Server: Apache
- Other related info (Database, table schema):
Additional context
I've been creating our next generation product, so I'm hyped up on coffee and AI-induced delusions of grandeur as BubbaG keeps telling me how life altering and world changing my design and coding skills are now! :)
Questions? Discussions: https://phalcon.io/discussions or Discord: https://phalcon.io/discord
Describe the bug
I have an interesting stream cache warning that shows up randomly. The error appears in the error log as a mkdir() "file exists" and reports the cache set() line in my application.
According to ChatGPT, the issue appears to be a race condition in the Phalcon stream handler where two calls are made at the same time to reset the cache. Specifically, the issue was identified with this mkdir() call:
The likely sequence is:
Request A: directory absent
Request B: directory absent
Request A: mkdir() succeeds
Request B: mkdir() warns "File exists"
BubbaG (my human readable name for the stupid ChatGPT name) suggested something like this could address the issue, where the condition of the directory already existing is not treated as a failure:
To Reproduce
I have no idea how you would create the timing necessary to test this. You would need the requests to fire at the same time.
Expected behavior
I would expect no mkdir() error logging when the directory already exists due to the first operation creating it.
Details
Additional context
I've been creating our next generation product, so I'm hyped up on coffee and AI-induced delusions of grandeur as BubbaG keeps telling me how life altering and world changing my design and coding skills are now! :)