[wp-trac] [WordPress Trac] #60855: wp_mkdir_p can error when simultaneous requests attempt to create the same directory

WordPress Trac noreply at wordpress.org
Fri May 17 22:53:48 UTC 2024


#60855: wp_mkdir_p can error when simultaneous requests attempt to create the same
directory
--------------------------+------------------------------
 Reporter:  grantmkin     |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by peterwilsoncc):

 Replying to [comment:2 siliconforks]:
 > I'm not sure I understand why it needs to "try a second time"?  If the
 directory has already been created, then nothing more would need to be
 done, right?

 This is to handle a [https://bugs.php.net/bug.php?id=35326 concurrency
 issue] in many of the versions of PHP WordPress supports.

 * Thread A needs to create `/path/to/uploads/2024/05`
 * Thread B needs to create `/path/to/uploads/fonts`
 * Thread A determines everything from `path` needs to be created
 * Thread A creates `path`
 * Thread B determines everything from `to` needs to be created
 * Thread B creates `to`
 * Thread A fails as `to` now exists
 * Thread A rechecks if `05` exists, it does not.
 * Thread B creates the remainder of the path to `fonts`

 The result being that one of the requests will still fail.

 > I'm not sure why the time sleeping would need to be random in that case.
 Actually, I'm not sure it would need to sleep at all.

 The back-off would be random in an attempt to avoid a second collision. If
 both threads wait the same amount of time, the path creation would collide
 a second time a few milliseconds later.

 Computer networks use [https://en.wikipedia.org/wiki/Exponential_backoff
 Exponential backoff] to try again and again, but I think this would be
 overkill for WordPress's purposes as there is a small risk the request
 would time out.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60855#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list