Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an exclusive lock when writing the cache file #205

Conversation

acinader
Copy link

fixes: #204

@acinader
Copy link
Author

I suspect that there's a dogpile issue here where, when using the lock, several other requests could queue up to write the cache, so the file could get written multiple times. Accounting for that would require a different strategy to lock both read and write through the whole critical section.

Having said that, I'd prefer a dog pile on the file to a corrupt file that makes the site unusable.

@nikic
Copy link
Owner

nikic commented Jan 16, 2020

This looks reasonable to me. We should probably also apply that to the 1.x branch.

@GrahamCampbell
Copy link
Contributor

GrahamCampbell commented Jan 16, 2020

There is still a bug here, in that it is possible to require a partially written file. The file should maybe instead be written to disk under a different name, and then renamed to the the real file.

@nikic
Copy link
Owner

nikic commented Jan 16, 2020

Yeah, that's a good point. I believes file renames on Linux at least are atomic.

@GrahamCampbell
Copy link
Contributor

Yeh, I think so too.

@prisis
Copy link

prisis commented Jan 16, 2020

Shouldnt it go a bit more in this direction https://github.com/webimpress/safe-writer/blob/master/src/FileWriter.php

@lcobucci
Copy link
Collaborator

@acinader
Copy link
Author

The reading of a partially written cache file explains errors I saw in our logs. Good callout @GrahamCampbell.

I don't think that atomic rename is a good full solution. It does not address the cold cache, dogpile problem where more than one request is building the cache file simultaneously.

I think that php's flock can do what we really want which is block reads while the cache file is being calculated once. I can take stab at it if that sounds reasonable?

FYI: I'm using Slim 3 which depends on FastRoute.

@acinader
Copy link
Author

acinader commented Feb 3, 2020

fyi, i am still getting corrupted cache files.

i'll try and capture a corrupt one and post details to the issue

@lcobucci
Copy link
Collaborator

Closing this in favour #200 (will be backported to v1)

@lcobucci lcobucci closed this Jun 14, 2021
@lcobucci lcobucci self-assigned this Jun 14, 2021
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.

File Cache Corruption and Exclusive Lock
5 participants