Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Race condition creating directory with New-Item?

I'm seeing a race condition when calling New-Item to create a directory on a foreign machine using a UNC path. The code is below:

New-Item $target -itemType Directory -Force -Verbose |
        %{ Write-Host "Creating dir" $_.FullName }

Using Test-Path immediately afterwards returns false. I put a Test-Path -> sleep for 1 second retry loop and after sleeping for 1 second, Test-Path is returning true.

Is New-Item a blocking call? Should I expect to have to wait after calling New-Item?

like image 644
Niall Connaughton Avatar asked Aug 11 '11 10:08

Niall Connaughton


1 Answers

I cannot reproduce your problem.

PS > New-Item "test" -itemType Directory -Force -Verbose | %{ Test-Path $_.FullName }
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Users\Frode\Desktop\test".
True

New-Item creates a new directory by getting a DirectoryInfo-object for the parent directory, and calling it's CreateSubDirectory, like:

DirectoryInfo subdirectory = new DirectoryInfo(parentPath).CreateSubdirectory(childName);

I'm not a developer, but AFAIK that means it's a blocking call, since it waits for an DirectoryInfo-object in return. So mabe the problem is with your storage subsystem.

like image 155
Frode F. Avatar answered Oct 01 '22 21:10

Frode F.