Файлы, исчезающие в середине функции

Интересная проблема, с которой я столкнулся сегодня, поэтому я пишу этот пост за советом о том, как справиться с этим сейчас и в будущем с другими частями моего приложения (и, возможно, помогать кому-то еще).

Рассмотрим этот метод:

protected function unlinkCachePath($cachePath) { if (!file_exists($cachePath)) { throw new \Exception("Invalid cache file path '$cachePath'"); } if (!is_writable($cachePath)) { throw new \Exception("Check permissions for file path '$cachePath'"); } $result = unlink($cachePath); // delete file if (!$result) { throw new \Exception("Problem deleting cache file '$cachePath'"); } return true; } 

Довольно просто, правда? Ну, оказывается, что функция unlink() прерывалась некорректно. Это было странно, потому что перед попыткой моего unlink() меня была file_exists() unlink() .

Но все же он генерировал ошибку «файл не найден». Поэтому я отправился на отладку, чтобы узнать, что происходит.

Отладка:

 protected function unlinkCachePath($cachePath) { // debug the cache path echo "testing cache path: $cachePath <br />"; // check if the file exists $this->debugFileExists($cachePath,'1'); if (!file_exists($cachePath)) { throw new \Exception("Invalid cache file path '$cachePath'"); } // ...check again $this->debugFileExists($cachePath,'2'); if (!is_writable($cachePath)) { throw new \Exception("Check permissions for file path '$cachePath'"); } // ...and again $this->debugFileExists($cachePath,'3'); $result = unlink($cachePath); // delete file // ...and again $this->debugFileExists($cachePath,'4'); if (!$result) { throw new \Exception("Problem deleting cache file '$cachePath'"); } return true; } 

 private function debugFileExists($filePath, $attemptNumber) { if (file_exists($filePath)) { $response = "pass"; } else { $response = "fail"; } echo "file_exists() test $attemptNumber: $response <br />"; } 

Результаты:

 testing cache path: /path/to/file.json file_exists() test 1: pass file_exists() test 2: pass file_exists() test 3: fail # unlink(file): No such file or directory file_exists() test 4: fail 

Чего ждать??

Вот я, почесывая голову. Как файл существует, а затем в той же функции внезапно не существует? Любое понимание того, что может произойти?

Спасибо.

Оказывается, бэкэндом API моего приложения является PHP, а интерфейс – угловатый. В редких случаях несколько интерфейсных вызовов API, которые попадают в unlinkCachePath($cachePath) , будут происходить одновременно – и в середине функции файл будет удаляться! Из-за этой конкретной проблемы проблема не была решена путем тщательного тестирования.