You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?php$gz = gzopen($cachefileb,'w9');
gzwrite($gz, $towrite);
gzclose($gz);
Resulted in this output:
Current function caused disk error while multiple sessions is writing to the same file.
But I expected this output instead:
can the file be locked while the file is being written?
### PHP VersionPHP7.4### Operating System
Almalinux
Thanks for the response!
Yes, someone is reporting the same problem on exactly the page you gave, has the issue been solved? https://www.php.net/flock#111443
The old code in use has already created a lot of data, fear rewriting the old code may unexpectly corrupt the old data.
new scripts are using the way you suggested.
Hope if can have something like gz_lock to patch to the old code.
This cannot be implemented for PHP < 8.5, and I would assume that upgrading to PHP 8 already requires quite some changes to your code base, so possibly tough luck.
Anyway, let's turn this into a proper feature request, since it generally makes sense to be able to lock zlib streams.
I found the problem perhaps can be solved by gz witing the file to tmp file then rename it back to the disk.
Even if the tmp file is corrupted when the lock failed, disk error can be prevented.
testing the code now, so far no disk error occurred.
Activity
cmb69 commentedon Feb 2, 2025
https://www.php.net/flock
githubyiti commentedon Feb 2, 2025
Thanks for the response!
Yes, someone is reporting the same problem on exactly the page you gave, has the issue been solved?
https://www.php.net/flock#111443
cmb69 commentedon Feb 2, 2025
No, that issue still persists. From a quick look at the implementation, the actual file stream is an "inner stream".
Workaround: use
fopen()instead ofgzopen()(and useini_set("zlib.output_compression", 9")).PS: probably better anyway: use a separate lock file (.lck).
githubyiti commentedon Feb 2, 2025
Thanks.
The old code in use has already created a lot of data, fear rewriting the old code may unexpectly corrupt the old data.
new scripts are using the way you suggested.
Hope if can have something like gz_lock to patch to the old code.
Regards,
cmb69 commentedon Feb 2, 2025
This cannot be implemented for PHP < 8.5, and I would assume that upgrading to PHP 8 already requires quite some changes to your code base, so possibly tough luck.
Anyway, let's turn this into a proper feature request, since it generally makes sense to be able to lock zlib streams.
[-]How to lock file when gzwrite file?[/-][+]zlib streams should support locking[/+]cmb69 commentedon Feb 2, 2025
Implementation would need to start at
php-src/ext/zlib/zlib_fopen_wrapper.c
Line 105 in ff80ec7
githubyiti commentedon Feb 2, 2025
Thanks for moderating the title.
Hope will have gz_lock in the future.
cmb69 commentedon Feb 2, 2025
Well, you would rather be able to use
flock().githubyiti commentedon Feb 5, 2025
Added an external file lock as below
not sure will the problem be fixed.
if(file_exists($cachefileb."-lock") and time()-filemtime($cachefileb."-lock")<3){@set_time_limit(10);sleep(rand(0,8));}
clearstatcache();
if(!file_exists($cachefileb."-lock") or time()-filemtime($cachefileb."-lock")>3){
touch($cachefileb."-lock");
$gz = gzopen($cachefileb,'w9');
gzwrite($gz, $towrite);
gzclose($gz);
unlink($cachefileb."-lock");
}
cmb69 commentedon Feb 5, 2025
No, I've meant just
flock()another file. Something like:githubyiti commentedon Feb 5, 2025
Thanks!
Seems your flock is better.
I just code for hobby and not familiar with the behavior of how flock works.
Will the below work?
$lock = fopen($gz . ".lck", "w+");
if(!$lock ){@set_time_limit(10);
sleep(rand(0,8));
$lock = fopen($gz . ".lck", "w+");}
if($lock ){flock($lock, LOCK_EX);
$gz = gzopen($cachefileb,'w9');
gzwrite($gz, $towrite);
gzclose($gz);
flock($lock, LOCK_UN);
fclose($gz . ".lck");
}
Implement phpGH-17668: zlib streams should support locking
Implement GH-17668: zlib streams should support locking
ndossche commentedon Feb 11, 2025
Implemented in PHP 8.5 via 5acff0e
githubyiti commentedon Feb 11, 2025
I found the problem perhaps can be solved by gz witing the file to tmp file then rename it back to the disk.
Even if the tmp file is corrupted when the lock failed, disk error can be prevented.
testing the code now, so far no disk error occurred.