file_put_contents() is racy
Personne n'a encore pris cette issue.
- Langage dominant
- C
- Étoiles
- 40.4k
- Forks
- 8.1k
- Merge moyen
- 2 j 13 h
- PR mergées (30 j)
- 96
Description
Imagine following sequence (two processes A and B writing some serialized data to file called "cache.dat" using file_put_contents():
- Process A: open the cache file for writing
- Process B: open the same cache file for writing
- Process A: truncate the file to zero length
- Process B: truncate the file to zero length
- Process A: write the cached data (e.g. 1002 byte long serialized data)
- Process B: write the cached data (e.g. 1000 byte long serialized data)
- Process A: close the file
- Procees B: close the file
This results in a file with 1000 bytes from Process B and 2 extra bytes from process A and you end up with unserialize(): Extra data starting at offset 1000 of 1002 bytes if you later try to unserialize the file.
Since it seems that many developers incorrectly believe that a function called file_put_contents() would actually write a file with the given contents, I'm suggesting that PHP internal implementation should instead be (unless FILE_APPEND or LOCK_EX in flags)
- Create a new temporary file in the same directory with the target filename.
- Write the given data (argument to file_put_contents()) into said file.
- Close the file
- Rename the temporary file to final target filename.
Since only the rename() is guaranteed to be atomic in POSIX compatible systems (and even that requires rename within the same directory), this is the only way to make sure you don't end up with mixture of two files when multiple processes are calling file_put_contents() at nearly the same time. And in case NFS is used, you cannot assume that LOCK_EX actually works so you must use rename() semantics.
And maybe allow current behavior with some new flag (FILE_ALLOW_RACY?) with documentation "Reduce syscalls to improve performance but caller must guarantee that two processes are not trying to write into the same file concurrently."
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez dans ext/standard/file.c vers la ligne 492 et suivez la gestion de file_put_contents() pour les écritures normales, FILE_APPEND et LOCK_EX. Examinez la séquence proposée de fichier temporaire dans le même répertoire et de renommage, puis déterminez le comportement requis et la couverture pour les écritures concurrentes, y compris toute alternative FILE_ALLOW_RACY et la documentation.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- c, php
- Domaine
- operating-systems
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100