multiple concurrent requests and the assets manager


I was wandering what will happen in the following use case: what happens if two requests are processed at the same time and both have in the code a section that publishes the same folder? will that result in 1 published folder? two? exception or some other error?

Does CAssetManager have mechanisms to handle such race condition situations?

As far as I’ve checked the code (very quickly) and searched in Google, I stumbled upon no reference that indicate handling of this situation. I’m sure more experienced people here can clear this out.



If I understand your question correctly, CAssetManager first checks if the publish directory exists and if not, creates it and publishes the assets. So there should be no collision. My hunch is that Apache (or other web server) or the OS handles a request to make a directory so is not a Yii/PHP specific issue.

If I understand you correctly, and I think I am :) , then surely there can be a collision:

say two identical request (same URL requested) are coming from two different clients. The two are processed by two different child processes of Apache. The first handled, causes CAssetManager to check if the directory exists and the answer is nope - so it creates one. In the meantime, the second request also run. It too checked if the directory exists - and the answer is nope - none was created when it checked indeed. Now it comes to create one, but in the short time between its check and the creation attempt, the first process already created it. This will return an error and I don’t know the internals of the CAssetManager to tell how this situation is handled. This is why I’m asking here (cannot dive into code).

This is something that cannot be determined by a framework. As per my original response, I believe it is the responsibility of Apache, the OS or possibly PHP itself to handle concurrency not Yii itself.

This is the actual directory creation code






I don’t have enough experience of PHP to tell for sure, but perhaps this would be better



  @mkdir($dstDir);   // remain silent about existing dir


    mkdir($dstDir);  // report possible path error




wk, you’re wrong:

Only in the application level you could tell about this race condition. All levels below it - PHP, Apache, Filesystem, etc are not aware of the full meaning of the error on the second ‘directory creation request error’. They are flagging about this error rightfully from their perspective.

This synchronization can surely be implemented by the framework and its a matter of decision whether it is to be done in the framework level or not.

Any member of Yii DEV team can comment on this?

I guess this is the right direction - a poor man’s synchronization by simply ignoring directory creation errors. Yet, it seems perfectly valid and as far as I can scratch my head right now - has no shortcoming to more complicated solutions.

See also: