Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-22
17:19 • Ticket [4f322b9d21] Impossibility to create an unique path in Tcl (e. g. using f... artifact: ba33e60b4b user: serhiy.storchaka
2026-10-15
11:39 • Closed ticket [4f322b9d21]. artifact: 6ca8405452 user: serhiy.storchaka
2018-10-10
22:41 • Ticket [4f322b9d21]: 3 changes artifact: 8c357b4f90 user: sebres
2018-07-12
13:51 • New ticket [3f7af0e21e] win: file delete could sporadic fail with "permissi... artifact: b079f464c5 user: sebres
09:43 • Ticket [4f322b9d21] Impossibility to create an unique path in Tcl (e. g. using f... artifact: c7fe9f4d49 user: aspect
2018-07-11
00:22 • Ticket [270f78ca95] file mkdir: race condition if two workers creates same direc... artifact: 3a4e4e651b user: sebres
2018-07-09
17:38 • Closed ticket [270f78ca95]. artifact: 3bc605cd57 user: sebres
2018-07-02
15:05 • Pending ticket [4f322b9d21]: Impossibility to create an unique path in Tcl (e. g... artifact: 46d09e9de4 user: sebres
14:46
Closes RFE [4f322b9d21]: provide return value for `file mkdir` to recognize the directories are real... check-in: 7e5504fecf user: sebres tags: rfe-4f322b9d21
12:12 • New ticket [4f322b9d21] Impossibility to create an unique path in Tcl (e. g. usi... artifact: 51847a2a1a user: sebres

Impossibility to create an unique path in Tcl (e. g. using file mkdir)

Closed / Rejected4f322b9d2112772a4a9599611def6e651f8e5198Created 8 years agoUpdated 41 days ago
Descriptionwiki2018-07-02 12:12:33
If working multi-threaded (or multi-processed in cluster-system on share) with some global directory to create some system-wide unique sub-path using some pseudo-unique name generation algorithm (also UUID), to avoid retrieving of the same folder for two workers, one can do the things like this:
proc get_unique_dir {} {
  while 1 {
    set p [file join $GLOB_PATH [some_uuid_algo]]
    if {![file exists $p]} {
      file mkdir $p
      break
    }
  }
  return $p
}

But it's affected by the race condition (firstly not exists for both workers, then one creates sub-path, another returns with no error in `file mkdir` on same existing path), if some_uuid_algo cannot guarantee a system-wide unique path.

So the creation of a safe construct for that is impossible in Tcl currently.

Suggestion:

Currently `file mkdir` returns no result at all.
If `TclFileMakeDirsCmd` will be extended with return value as counter (of created tail-directories), one may use it to avoid race condition in above-mentioned script, like here:

proc get_unique_dir {} {
  while 1 {
    set p [file join $GLOB_PATH [some_uuid_algo]]
    if {[file mkdir $p]} {
      break
    }
  }
  return $p
}

This is race condition safe. The only one backwards-incompatibility in this case would be usage like this:

set some_var "when [file mkdir $some_path]was used in substitution"
But I believe the way like this is very rarely (or almost never) used.
Referenced by4
  • b079f464c5New ticket [3f7af0e21e] win: file delete could sporadic fail with "permission denied". ticket2018-07-12 13:51:21
  • 3a4e4e651bTicket [270f78ca95] file mkdir: race condition if two workers creates same directory and one worker deletes it immediately status still Closed with 5 other changes ticket2018-07-11 00:22:24
  • 3bc605cd57Closed ticket [270f78ca95]: file mkdir: race condition if two workers creates same directory and one worker deletes it immediately plus 6 other changes ticket2018-07-09 17:38:03
  • 7e5504fecfCloses RFE [4f322b9d21]: provide return value for `file mkdir` to recognize the directories are really created, that can be used to avoid race condition by creating system-wide unique path (with multiple workers). See https://core.tcl-lang.org/tcl/info/4f322b9d2112772a for example. check-in2018-07-02 14:46:12
sebreswiki2018-07-02 15:05:03

Implemented in branch rfe-4f322b9d21 for >= 8.5th.

Although I think, it could be accepted as it's, adding a short changelog entry with small incompatibility note like: command `file mkdir` returns integer now vs. nothing previously... But TCT should decide about it.

So pending as implemented.

aspectplain2018-07-12 09:43:10
I approve of this change.  Can we afford to break code that relies on the
empty result from [file mkdir]?

See also https://core.tcl.tk/tips/doc/trunk/tip/431.md and the ensuing
wiki discussion at https://wiki.tcl.tk/40609

https://core.tcl.tk/tcl/info/270f78ca95b642fb is also relevant.
sebreswiki2018-10-10 22:41:54

So [270f78ca95b642fb] is definitely closed, well it is time to "close" another race-condition (this RFE fixed).
Someone with an idea to bring this thing further?
I spoke to several people: no one has used [file mkdir] to get/check empty return value / or somehow expected empty values in string like "...[file mkdir]..." or similar imaginable substitutions.
Any suggetions, proposals, etc?

serhiy.storchakawiki2026-10-15 11:39:52

Never merged (the return value change would need a TIP); in Tcl 9.0 [file tempdir] from TIP 431 creates a unique directory atomically.

serhiy.storchaka Ticket [4f322b9d21]: 3 changes [History diff]2026-10-22 17:19:27
Log in (anonymous is fine) to add a comment.