neděle 13. října 2019

PHP: flock and fwrite is not save, you can lose data

This is the same case as file_get_contents and file_put_contents. With all these functions you can lose data if multiple script are running same time, trying to read and write, esspecially write, the same file.

You can find some mentions here:

PHP flock() alternative

A locking file cache in PHP

I have made lots of tests using all possible methods:
1. file_get_contents/file_put_contents with LOCK_EX flag used in  T2-T3
2. fopen, fflock, fread, fclose, fopen, flock, fwrite, fflush, fclose in T5-T6
3. the same as in point 2, but without flock, using mkdir lock instead (T8, T9)

Please see the results here:

PHP: Results comparison of file_ (get) put_contents & f (read / write / flush)
The discussion in located on Czech server:

PHP: Výsledky srovnání funkcí file_(get)put_contents & f(read/write/flush)

So you can see clearly, that none of these methods is reliable, but - this is important - this is unders certain very rare circumstances. This test depends on how many circles you make and how many scripts you run. I run 4 scripts (made 1 script in real, but this runs after 4 requests were made from different browsers). So you may minimize the lock by descriting the number of the loops from 50 to 10 or 6 and you can do the same if you run only 3 scripts. Nobody will run such script in real, this was only to try simulate hard traffic. Of sure this is not the same as to run DOS attack using JS loop sending requests to server or some more realistic remote action.

Also please notice you have another articles regarding the atomicity on this blog, I have written them in English.

1 komentář:

Python: Rozdíl mezi přidáním a rozšířením pole - append vs extend'!

  Výborná otázka – pojďme si vysvětlit rozdíl mezi append() a extend() a proč je v tomto konkrétním případě správné použít extend() . ...

Štítky

.profile adm administrace Adobe AI akcelerace alfa transparence analýza AND any aplikace apt ar archiv asociativní pole atomicity audio autentifikace awk balíčkovací systém bash beacon beacon_hint benchmark Bézierovy křivky bezpečnost biblehub BJT blogger boolean buffer buffering Cache-Conrol Cloudflare code Collector Cut-off ColorManager colorpicker common compare config cookies CPU CPU pipe css CSS3 curl current code cut čas data loss data lost data transfer reliability datasheet datetime.strptime deb deb-systemd-helper debian debián depricated development dioda diody dpkg dpkg -S dpkg-deb drivers EBO Emitter Cut-off Current eps ETag exclude exec Expires extrakce jediného extrakce názvu balíčku souboru extrakce obrázků extrakce souboru .deb fflock fflush ffmpeg FIFO file read file write file_get_contents file_get_contents/file_put_contents file_put_contents filter find first_install.sh flock Fly-back dioda font-face fóra fotorezistor fread functions funkce fwrite gate gate drive GDVfs gedit gedit-common geolokace getdata Ghostscript GIO glib gnome gnome settings GNU Privacy Guard gnupg gpg gradient-background grafika grep grep -v groupadd grub grub update gs gsettings gtk gtk.css gtk+ hebrejština history hlavičky HS html html 5 https hudba charakterizace chatGPT chroot chyba ICES IGBT Image img sizes img srcset impedance implementace imshow inference inkscape inrush current install jalový výkon javascript javescript jednocení seznamů js jsonData kapacita součástek koeficient zesílení komponenty xFce komunikace se serverem koncept konfigurace kontejner korekce barev Krita KSF Last-Modified lazy caching led LEFT JOIN librosa ligatury light-locker lightdm linux list log maják manuál maskování maskování služby masky matplotlib Max-Age measure memory měření MFCC MFCC koeficienty mint Mint 21.3 Mint xFce míry modules moralizace morphologie MOSFET mount moviepy mysql náběhový proud napěťová ochrana nastavení šablony návod nel Network Error Logging NLP normalizace šedi po resize not Notifications NTFS nth-child oblasti oblékání ochrana okruhy přátel OpenVINO IR formát oprava oprava balíčku optočlen org.gnome.desktop.screensaver org.gnome.nm-applet ořezové masky OSHB otázky otázky_jazyky otázky_moralismu_řešení ovladače panely parsování path pdf personifikace photorec php php 4 php 5 php 6 php 7 php 8 phpbb phpBB3 PipeWire pitch PN přechody pnp pole Policykit postscript práva profilování program prune průraz přeinstalování přepěťová ochrana přepolování příkazy připojení k síti připojení k wifi pseudokódd pstoedit PulseAudio PWM regulátory pydub python python3 pytorch ramdisk RBE RDSon read reaktance rectifier regulace vstupního napětí reinstall relyability remount replace restore reverzní geolokace RIGHT JOIN rm role rozvržení disků pro OS linux a data databází řešení samba scan scroll sdílení sdílení souborů Sec-Fetch-Dest Sec-Fetch-Mode Sec-Fetch-Site Sec-Fetch-User Secure Shell sed Set Cookie show-manual-login show-remote-login shunt schemas schémata schottka skript skupiny sloupce služby small song sort soubory soundfile spínané zdroje spínání splines split správa diskových zařízení SQL ssh stabilizace napětí stahování stíny stream string strojové učení stropové učení supplicant svg syntax systemctl systemd-logind T5 tabulka tabulky Tangentové úsečky tar témata tepelná ztráta terminologie test text-shadow themes thermal runaway time timestamp tkinter tr transistor transition tranzistor tranzistory tuple tvorba otázek TVS ubuntu účiník udiskd udisks unconfined underrun unity-greeter update usermod uživatelé va charakteristika vala vektorová grafika Vgs video Vth vynechání adresářů vývoj while wpa wpa_supplicant wrapovací funkce x xandr xapp-watt xargs -I xed xed-common xfdesktop xml XOR Xorg Xorg Thumbnails xrandr závislosti zdánlivý výkon zdroj zenerka zenerovo napětí zip zip archiv zkratky zpomalení zpracování textu zrychlení Žalmy