Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2018-10-10
22:41 • Ticket [4f322b9d21] Impossibility to create an unique path in Tcl (e. g. using f... artifact: 8c357b4f90 user: sebres
2018-07-16
17:12 • Ticket [270f78ca95] file mkdir: race condition if two workers creates same direc... artifact: aab0797683 user: anonymous
2018-07-12
10:42 • Closed ticket [270f78ca95]. artifact: e9c90bbf01 user: sebres
10:35 • Ticket [270f78ca95]: 3 changes artifact: 1ad0a2b9c0 user: sebres
10:33 • Ticket [270f78ca95]: 3 changes artifact: 858dfd8cbf user: sebres
10:21 • Open ticket [270f78ca95]. artifact: 82c7c42d7f user: aspect
2018-07-11
00:22 • Ticket [270f78ca95]: 5 changes artifact: 3a4e4e651b user: sebres
2018-07-10
11:29 • Ticket [270f78ca95]: 5 changes artifact: 2e9e359589 user: anonymous
2018-07-09
17:38 • Closed ticket [270f78ca95]. artifact: 3bc605cd57 user: sebres
17:18
closes [270f78ca95b642fb]: fix the race condition for `file mkdir` if some worker deletes directory ... check-in: 1c12ee9e45 user: sebres tags: core-8-5-branch
16:07 • Ticket [270f78ca95] file mkdir: race condition if two workers creates same direc... artifact: 11aaec0d0a user: sebres
16:03 • Ticket [270f78ca95]: 3 changes artifact: b49bfbc840 user: sebres
15:13 • Open ticket [270f78ca95]. artifact: a0d2eb2d15 user: anonymous
15:12 • Ticket [270f78ca95]: 5 changes artifact: 2a257d79f7 user: anonymous
13:16 • Closed ticket [270f78ca95]. artifact: 973a9279ee user: sebres
11:46 • Ticket [270f78ca95]: 3 changes artifact: d1e2d13d3f user: anonymous
11:41 • Ticket [270f78ca95]: 3 changes artifact: 83d5a7cdc3 user: anonymous
11:40 • Ticket [270f78ca95]: 4 changes artifact: 72ecdfad43 user: anonymous
11:38 • New ticket [270f78ca95]. artifact: e6e8e96b55 user: anonymous

file mkdir: race condition if two workers creates same directory and one worker deletes it immediately

Closed / Fixed270f78ca95b642fbed81ed03ad381d64a0d0f7dfCreated 8 years agoUpdated 8 years ago
Descriptionplain2018-07-09 11:38:43
When running in parallel two terminals doing:

 1. while {1} { file mkdir bla }
 2. while {1} { file mkdir bla; file delete bla }

mkdir eventually fails. On ext4 this is not as bad though it happens (and breaks delete sometimes as well), but on nfs mkdir consistently fails when two process make the same dir. This does not happen using 

    exec mkdir -p bla

Related SO question with some tests results of mine:
https://stackoverflow.com/questions/48095178/atomicity-of-mkdir
anonymous Ticket [270f78ca95]: 4 changes [History diff]2018-07-09 11:40:41
anonymous Ticket [270f78ca95]: 3 changes [History diff]2018-07-09 11:41:43
anonymous Ticket [270f78ca95]: 3 changes [History diff]2018-07-09 11:46:20
sebreswiki2018-07-09 13:16:37

This reace condition was already fixed in [27b6822849], so I'll close it as duplicate. Please reopen if I'm wrong.

anonymousplain2018-07-09 15:12:33
This can reproduced with mkdir alone. On nfs with 3 terminals, 

 1. bash : while [[ 1 ]]; do rm -rf bla; done
 2. tclsh: while {1} {file mkdir bla}

This will already cause bash to throw weird errors (but loop will continue)
 
 rm: cannot remove `bla': Is a directory

even though we used -rf. Now add
 3. tclsh: while {1} {file mkdir bla}

Almost immediately one of the tcl shells will produce:

 can't create directory "bla": file already exists

These things are tcl specific, and won't happen with builtin mkdir -p.
anonymous Open ticket [270f78ca95]. [History diff]2018-07-09 15:13:11
sebreswiki2018-07-09 16:03:29

Hmmm, you're right.

Corresponding the code this can indeed happen, but this time as dual race condition:

Worker 1Worker 2Worker 3
Tcl_FSCreateDirectory (mkdir) succeedsTcl_FSCreateDirectory (mkdir) fails with EEXIST-
return TCL_OKTcl_FSStat and check is S_ISDIRdeleted the directory
-not exists, so return TCL_ERROR-
This was introduced (even as race condition prevention;) in d0f1daac754e8554. But ATM it produces another race condition (as worker 3 deletes the directory and worker 2 doing the stats check)...

I'll fix it, thanks! WiP.

sebres Ticket [270f78ca95]: 4 changes [History diff]2018-07-09 16:07:05
sebreswiki2018-07-09 17:38:03

Fixed in [1c12ee9e45222d6c], thus close.

Although the case is very strange (the delete implicitly after create is IMHO not good way in any case, because can cause a lot of other race conditions), but I could imagine situations where it may be expected resp. could occur once (e. g. the three-point delete/create/create).

So this one is fixed now (under assumption the time of creation inside one worker is important and if another worker deletes the folder, each worker has exactly one attempt to repeat the creation. Hereafter it gets off with the success, if it's a tail (despite the directory may not exist), or fails deeper on create of child folders if it was not a tail directory.

Other imaginable race conditions on mkdir are unwanted resp. impossible to fix and should be resolved another way's (like RFE [4f322b9d21]).

anonymousplain2018-07-10 11:29:56
Thanks for the super fast fix. I'd like to comment that this was originally discovered with **no delete at all** - our daily regressions running in parallel would sometimes create directories at the same place (which doesn't matter for our purposes) but would often fail. I think the fact this is a NFS file system made the race condition prevalent (and the error is different compared to ext4 when removing).
sebreswiki2018-07-11 00:22:24

So I assume it works now for you also as regards your NFS (NAS share)?

But what I don't understand, how it can fail without delete at all (especially with EEXIST error code). Possibly you use something like temporary folders (just with range of the same names)? So it could be deleted (after work done)? In case like this I think to resolve such time-issues (and they are rather a time- as a race-condition indirectly) you should possibly add some business logic (e. g. with some time-component in the name of the folder) to avoid deleting of still "active" directory (as already said like described in RFE [4f322b9d21]). Note, that in this case both creater-workers will return successfully, but folder will be not exist (because deleted) so you'll get a consequential error just later.

Also note on NFS (especially under control of Windows) there are some rude antivirus or other IDS-similar software's (could use quarantine, worker suspending and co), that may cause similar errors there.

aspectwiki2018-07-12 10:21:53

I'm not convinced by [1c12ee9e45222d6c]. If I'm reading the patch correctly ..

If EEXIST is raised twice, TclFileMakeDirsCmd simply ignores it. The comment above "goto nextPart" suggests something different, so I suspect this isn't intended.

Removing the call to FSStat means we can report success if the target exists and is a file. This is clearly wrong.

Removing the Tcl_ResetResult() call rings alarm bells, but it looks like that was redundant in the first place.

I think lines 287-289 (post-patch) should simply be deleted so the success case can pass through Tcl_FSStat success on line 258.

sebresplain2018-07-12 10:33:23
If EEXIST is raised twice, it can only be something (which going possibly deleted inbetween), because otherwise (it's a file) it will exit with error in [./artifact?udc=1&ln=259-263&name=ee3766c8dd773316|!S_ISDIR(statBuf.st_mode)].
Endless cycle is not needed (would be too dangerous).
sebreswiki2018-07-12 10:35:03

I mean in !S_ISDIR(statBuf.st_mode)...

P.S. wiki should be really the default remark format.

sebres Closed ticket [270f78ca95]. [History diff]2018-07-12 10:42:23
anonymousplain2018-07-16 17:12:13
Sorry guys, stupid GMAIL sent mails from this thread to spam:

Definitely fixed, 3 terminals on NFS:

 1. tclsh: while { 1 } { file mkdir bla }
 2. tclsh: while { 1 } { file mkdir bla }
 3. bash : while [ 1 ]; do rm -rf   bla done

running 5 minutes straight and no errors on the tcl shells. The bash shell reports errors, but that's expects as there is a race condition with rm (which propagates to file delete I guess).

Looks really fixed on my side.
Log in (anonymous is fine) to add a comment.