[SERVER-57731] File/folder may be locked for delete on Windows Created: 15/Jun/21  Updated: 03/Sep/21  Resolved: 03/Sep/21

Status: Closed
Project: Core Server
Component/s: Concurrency
Affects Version/s: 5.1 Required
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Sergey Galtsev (Inactive) Assignee: Sergey Galtsev (Inactive)
Resolution: Won't Fix Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Operating System: ALL
Sprint: Security 2021-09-06
Participants:
Linked BF Score: 40

 Description   

Windows may not allow for immediate file deletion thus causing an exception. An example of this is in BF-13075 and BFG-858391. Specific error is in src/mongo/shell/shell_utils_launcher.cpp in ResetDbpath, where Windows exceptions are handled by sleep followed by retry. This method reduces number of failures, but does not fully eliminate them

Per https://docs.microsoft.com/en-us/windows/win32/fileio/closing-and-deleting-files , there is an alternative way of deleting an object: it has to be renamed and then a DeleteFile function invoked ("The DeleteFile function marks a file for deletion on close"). This would produce an empty directory, and the old one would be eventually gone

Before starting work on this ticket, need to determine if this could be done using standard c++ or boost, whithout expressly invoking win32 functionality.



 Comments   
Comment by Sergey Galtsev (Inactive) [ 03/Sep/21 ]

will not do it, since solution as-described doesn't work, and the problem is too rare to bother fixing

Comment by Sergey Galtsev (Inactive) [ 31/Aug/21 ]

Following code fragment was used to override the locked directory condition on windows; I tested, but it didn't solve the problem:

#ifdef _WIN32
            try {
                sleepmillis(100);
                boost::filesystem::remove_all(path);
            } catch (const boost::filesystem::filesystem_error&) {
                const auto newExt = "." + std::to_string(time(nullptr));
                boost::filesystem::path newPath(path);
                newPath.replace_extension(newExt);
                LOGV2_WARNING(5773100, "ResetDbpath(): Directory locked, will try to rename",
                    "path"_attr = path, "newPath"_attr = newPath.string());
                boost::filesystem::rename(path, newPath);
            }
#else

Generated at Thu Feb 08 05:42:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.