[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
Wed Mar 27 18:54:56 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        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When two requests happen at the same time that both attempt to create a
 new directory with `wp_mkdir_p`, a race condition can occur that results
 in one request creating the directory and the other triggering an error.

 The issue happens when both of the requests reach the `file_exists` check
 at nearly the same time:

 https://github.com/WordPress/wordpress-
 develop/blob/e73db80196c0044503633bda41511258a9516a8b/src/wp-
 includes/functions.php#L2059

 The directory does not exist yet, so the check fails and both requests
 continue execution of the function. Then the requests are in a race to get
 to the code where the directory is actually created using `mkdir`:

 https://github.com/WordPress/wordpress-
 develop/blob/e73db80196c0044503633bda41511258a9516a8b/src/wp-
 includes/functions.php#L2082

 The first request to reach that point will successfully create the
 directory and return true, the second will return false when `mkdir` fails
 because the directory already exists.

 This causes an error upstream in the code because `wp_mkdir_p` is expected
 to return true when the directory already exists.

 Found when testing the font library on new installs, see
 https://github.com/WordPress/gutenberg/issues/59023#issuecomment-2022746082

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


More information about the wp-trac mailing list