0

( reported here https://superuser.com/questions/1511981/windows-10-creating-and-deleting-directory-in-remote-location-netapp-in-a-loop this time with some more data and example program )

I am working on an issue - Create and deletion of directory on a loop in a small window gets incorrect response from API - it says directory deleted (returns true) but it stays there for some more time(say 20-30 milliseconds). What can cause? The program runs fine on Windows 7 (NetApp storage or local disk). Windows 10 passes with local disk; but fails with NetApp storage. Need to sleep get the API (DeleteDirectory correct )

Wrote a code to reproduce in windows , c program (because admins wanted that way to rule out java, powershell etc )

#include "pch.h"
#include <iostream>
#include <cstdlib>

#include <windows.h>
#include <direct.h>

using namespace std;
int main()
{

    const char* sleepTimeAsString = std::getenv("SLEEP_TIME_MILLIS");
    const char* remoteRoot = std::getenv("REMOTE_UNC_PATH");
    if (sleepTimeAsString == NULL) {
        std::cout << "env  SLEEP_TIME_MILLIS needed: " << '\n';
    }
    if (remoteRoot == NULL) {
        std::cout << "env  REMOTE_UNC_PATH needed: " << '\n';
    }

    std::cout << "set REMOTE_UNC_PATH=" << remoteRoot  << '\n';
    std::cout << "set SLEEP_TIME_MILLIS=" << sleepTimeAsString << '\n';

    CreateDirectory(remoteRoot,NULL);
    std::cout << "version 0.10 "  << '\n';
    std::cout << "created " << remoteRoot << '\n';
    string path = remoteRoot;
    string mynode = "/childdirectory";
    string path2 = path + mynode;


    DWORD error=  GetLastError();
    std::cout << error << '\n';

    long sleepTime = atol(sleepTimeAsString);
    for (int i = 0; i < 1000; i++) {
        std::cout << "loop "<< i << '\n';
        if (!CreateDirectory( path2.c_str(), NULL)) {
            DWORD errorlooped = GetLastError();
            std::cout <<"failed to create " << errorlooped << '\n';
            exit(-1);

        }
        else {
            std::cout << "success created " << path2.c_str() << '\n';
        }

        Sleep(sleepTime);


        if (!RemoveDirectory(path2.c_str())) {
            DWORD errorlooped = GetLastError();
            std::cout << "failed remove "<< errorlooped << '\n';
            exit(-1);
        }
        else {
            std::cout << "success deleted " << path2.c_str() << '\n';
        }
        Sleep(sleepTime);
    }
}

Result table: Once I provide sufficient sleep between calls the test started passing on this NetApp directory.

OS   Disk             Sleep Time(ms) Result 
Win7 Local            0              PASS
Win7 Remote           0              PASS

WIn10 Local           0              PASS
WIn10 Remote (NetApp) 0              FAIL
WIn10 Remote (NetApp) 10             FAIL
WIn10 Remote (NetApp) 20             FAIL
WIn10 Remote (NetApp) 40             PASS 

What should I investigate more on this issue (before getting back to System administrators ) - Ideas welcome.

Jayan
  • 18,003
  • 15
  • 89
  • 143
  • 1
    Did you read the [documentation](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectoryw)? *"The RemoveDirectory function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed."* – IInspectable May 15 '20 at 13:59
  • Could you see any problem in sample code? No open handles. More over why does time play role? – Jayan May 15 '20 at 16:04
  • 1
    There's no issue in the code. The issue is in the expectation, that a directory delete would instantly materialize. Your code doesn't run in a vacuum. There's an OS around it, with anti-malware systems installed, or infrastructure like the Recycle Bin. Lots of opportunities for other processes to hold on to a file system resource you would want to see gone. – IInspectable May 15 '20 at 16:10
  • same code works in Windows7. Major changes are around SMB protocol and implementation. (I will test with AntiVirus turned off - thank you) – Jayan May 15 '20 at 17:24

0 Answers0