When CreateDirectory returns ERROR_ACCESS_DENIED and should not

My Win32 app A1 (actually a set of processes) tries to use CreateDirectory to create the D1 directory in the parent directory of P. The path to P is the value of the TMP environment variable, which makes P a potentially busy, but generally permissive. In most cases, everything works fine, but rarely, CreateDirectory fails, and GetLastError returns ERROR_ACCESS_DENIED , the value of which is not documented in this context.

I wrote a test application A2 that does nothing, but I create and delete the D2 directory several times as fast as possible in P, and I chose a silly long name for D2, which I am sure will not encounter any other program will use. Every few minutes, a small part of a second occurs, during which A2 tries to create D2, it only gives ERROR_ACCESS_DENIED failures.

A1 at the time of its launch becomes quite busy inside P. Although A1 and A2 work at the same time, periods of ERROR_ACCESS_DENIED failures occur somewhat more often, as if A1 and A2 competed for exclusive access to P. (I am absolutely sure that A1 does not use the same name as D2. :-)

I am a little inclined to take ERROR_ACCESS_DENIED to mean โ€œtry again in a few milliseconds, and if it doesnโ€™t work after a few attempts, give up,โ€ but Iโ€™m worried that [a] in some this may mean something permanent that I should listen immediately , and [b] because I really donโ€™t know what is going on, it may not be possible to confidently set a reasonable amount of time to continue trying.

Does anyone have any experience? Any advice? Of particular importance at this point would be the key to what causes this, so I can more easily reproduce the problem.

+2
source share
1 answer

You are dead. the documentation does not even list ERROR_ACCESS_DENIED as a possible error code for this function, so it could be an error.

I would do as you suggest in implementing a retry / snooze strategy.

In other words, if you get this error, try again up to three times without delay (obviously, stop at any point here if you get a return code without errors), then up to four times with delays (for example, 100 milliseconds, 500 milliseconds, 1 second and 2 seconds).

This strategy (which I used before) usually bypasses any temporary lack of resources. If you still cannot create the directory after 7 attempts and 3.6 + seconds, you can probably safely assume that this will not happen.

Your function may be as ugly as (pseudo-code):

 def createMyDir (dirname): if createDir (dirName) return true; if createDir (dirName) return true; if createDir (dirName) return true; sleep (100) if createDir (dirName) return true; sleep (500) if createDir (dirName) return true; sleep (1000) if createDir (dirName) return true; sleep (2000) return createDir (dirName); 

but you might want you to make it a little more elegant:

 def createMyDir (dirname): delay = pointer to array [0, 0, 0, 100, 500, 1000, 2000, -1] okay = createDir (dirName) while not okay and [delay] not -1: if [delay] not 0: sleep ([delay]) delay = next delay okay = createDir (dirName) return okay 
+1
source

All Articles