ZF-5585: Zend_Config_Writer_Ini fails to write config sometimes

Description

Hi all!

This is my first bug-report to ZendFramework project, so first of all, let me thank all ZF-team for developing such a nice free product!

I have recently encountered a following problem with Zend_Config_Writer_Ini: method write() sometimes could fail with exception Zend_Config_Exception('Could not write to file "...), if there are a lot of consecutive config writes within a small period of time (3-5 seconds).

I wrote some code yesterday, that is executed by CRON and can run longer that php timeout allows. To get round that timeout, I've written a code to broke the job into several parts and store the state info into config file with Zend_Config_Writer_Ini. When I tested the code, sending manual http requests, I noticed that sometimes Zend_Config_Writer_Ini->write() was throwing an exception Zend_Config_Exception('Could not write to file "...). I think, it happens, because file_put_contents() function, that is used by Zend_Config_Writer_Ini->write(), does some output buffering and when it processes a lot of consecutive config writes (from different script runs), there are some collisions with file write access, that leads to file_put_contents() failure. When I rewrited the code , that does config file processing, with ordinary fopen-flock(LOCK_EX)-fwrite-fflush-fclose functions, the problem disappeared.

The bug is hard to reproduce - I tried to reproduce it today to test if file_put_contents(,,LOCK_EX) would solve the problem, but I failed - Zend_Config_Writer_Ini worked fine. But this could also mean, that filesystem cache (I use Apache2 on WindowsXP) was relatively fresh that time and able to prevent such collisions.

I think, there are at least two possible solutions: 1) rewrite Zend_Config_Writer_Ini->write() with fopen-flock(LOCK_EX)-fwrite-fflush-fclose functions. It guaranties that the file will be written correctly, or 2) make an option for Zend_Config_Writer_Ini to pass to file_put_contents() function the LOCK_EX flag. In theory, it should also help in such situations, but I failed to test it.

BTW: the bug could also exists in different Zend_Config_Writer_* classes, that uses file_put_contents().

Comments

The second solution sounds good, I will implement that soon.

Fixed with r13708 and merged into 1.7 release branch with r13709.

I'm impressed how fast the issue was resolved :) Thanks for your work! :)

correct fix version