Skip to content

Need atomic write when preparing code #61

Open

Description

@alantum

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

Activity

  1. Ocramius commented on Jul 4, 2018

    @Ocramius
    Member

    Would adding a LOCK_EX solve this?

  2. alantum commented on Jul 4, 2018

    @alantum
    Author

    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.

  3. Ocramius commented on Jul 4, 2018

    @Ocramius
    Member

    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)

  4. alantum commented on Jul 4, 2018

    @alantum
    Author

    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

  5. shivashanti commented on Feb 10, 2019

    @shivashanti
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions