Skip to content

Conversation

@sartor
Copy link
Contributor

@sartor sartor commented Nov 7, 2022

Q A
Is bugfix? ✔️
New feature?
Breaks BC?

On high load projects sometimes there is an error trying to delete file. This happens because of not atomic check if file exists and actual delete:

if (!is_file($file)) {
    return true;
}

return @unlink($file);

In old yii2 file cache result of key delete not throw any error.
This PR tries to return false only if unlink call error caused by any reason except file already deleted
I can't do simple test to prove fix, but I've checked it by calling first unlink just after is_file() check

@what-the-diff
Copy link

what-the-diff bot commented Nov 7, 2022

  • Added a check to see if the file was already deleted by another process on high load.
  • If it is, then return true and clear last error message so that we don't get an exception thrown in our codebase for this case.

@codecov
Copy link

codecov bot commented Nov 7, 2022

Codecov Report

Base: 95.74% // Head: 93.19% // Decreases project coverage by -2.54% ⚠️

Coverage data is based on head (10ff8f1) compared to base (6c0eda6).
Patch coverage: 42.85% of modified lines in pull request are covered.

❗ Current head 10ff8f1 differs from pull request most recent head b59ca98. Consider uploading reports for the commit b59ca98 to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #50      +/-   ##
============================================
- Coverage     95.74%   93.19%   -2.55%     
- Complexity       66       68       +2     
============================================
  Files             1        1              
  Lines           141      147       +6     
============================================
+ Hits            135      137       +2     
- Misses            6       10       +4     
Impacted Files Coverage Δ
src/FileCache.php 93.19% <42.85%> (-2.55%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@rustamwin
Copy link
Member

Need tests.

@sartor
Copy link
Contributor Author

sartor commented Nov 8, 2022

Need tests.

Any ideas how it can be done?

@samdark
Copy link
Member

samdark commented Nov 10, 2022

It's hard to test race conditions. I think we can skip these for the case. @sartor would you please add a line for CHANGELOG?

Copy link
Member

@vjik vjik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@vjik
Copy link
Member

vjik commented Nov 10, 2022

Seems, similar problem may occur in method removeCacheFiles():

if (!$expiredOnly && !@rmdir($fullPath)) {

} elseif ((!$expiredOnly || @filemtime($fullPath) < time()) && !@unlink($fullPath)) {

@samdark
Copy link
Member

samdark commented Nov 10, 2022

Yes, likely.

@sartor
Copy link
Contributor Author

sartor commented Nov 10, 2022

Yes. I'm using on prod my fork for a week, and fixed line weren't produced any new errors.
But there are similar errors left in other places. I'll fix them later in another PR

@vjik vjik merged commit 93bf2fb into yiisoft:master Nov 13, 2022
sartor pushed a commit to sartor/cache-file that referenced this pull request Dec 2, 2022
sartor pushed a commit to sartor/cache-file that referenced this pull request Dec 2, 2022
sartor pushed a commit to sartor/cache-file that referenced this pull request Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants