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

Need atomic write when preparing code #61

Open
alantum opened this issue Jul 4, 2018 · 5 comments
Open

Need atomic write when preparing code #61

alantum opened this issue Jul 4, 2018 · 5 comments
Labels

Comments

@alantum
Copy link

alantum commented Jul 4, 2018

in line PHPTAL.php:828 used not atomic writes via file_put_contents
if (!file_put_contents($this->getCodePath(), $result)) {

OpCache in some cases is in time to store an empty file into the cache. Potential reasons for it is high RPS, high concurency, slow disks and so on.

File exists, it's not empty, there are no errors while requiring file, but function is not defined and we have this log message:
E_ERROR: Call to undefined function tpl_... in: /var/local/www/app/outsource/PHPTAL/PHPTAL.php:667

@Ocramius
Copy link
Member

Ocramius commented Jul 4, 2018

Would adding a LOCK_EX solve this?

@alantum
Copy link
Author

alantum commented Jul 4, 2018

http://php.net/manual/en/apc.configuration.php#ini.apc.file-update-protection

When a file is modified on a live web server it really ought to be done in an atomic manner. That is, written to a temporary file and renamed (mv) the file into its permanent position when it is ready.

@Ocramius
Copy link
Member

Ocramius commented Jul 4, 2018

apc is really really really really really really to be avoided. It is unstable, unmaintained and can lead to random segfaults due to internal memory corruption. If the file has to be written atomically, an exclusive lock may work (although it may lead to some errors while multiple client re-generate the cache: dogpiling-alike)

@alantum
Copy link
Author

alantum commented Jul 4, 2018

I took APC as good documented example. In real project we use OpCache https://secure.php.net/manual/en/opcache.configuration.php#ini.opcache.file_update_protection

@Potherca Potherca added the triage label Feb 7, 2019
@shivashanti
Copy link
Contributor

I think it will improve even with LOCK_EX, Probably not enough for LOCK_EX. 
The code generating process needs to create a temporary file and mv it.
Because there is a very very short time from file open to lock. If other process read the file at that time, it will appear as an empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants