rmdir frequently fails with the 'MATLAB:RM​DIR:SomeDi​rectoriesN​otRemoved' error

12 visualizaciones (últimos 30 días)
I'm using matlab.unittest.fixtures for unit testing with matlab.unittest.TestCase. The operation involves file conversions, so I think it makes sense to use "setup" method for preparing a work folder and "teardown" for deleting the same folder.
The removal of the folder is done by rmdir function. It works fine, but 3 to 5 times per 10 trials, it fails to delete the folder with an error message 'MATLAB:RMDIR:SomeDirectoriesNotRemoved'.
This is essentially about the same issue, but putting rehash() after rmdir did not help. It's just unpredictable at the moment. I checked the folder's write permission with fileattrib function. Write permission is surely granted, but it still fails.
Only to illustrate the point, I wrote much simpler code. Interestingly, I successfully reproduced the problem.
clc;
str = 'TEMP_FOLDER';
for i = 1:100
if isdir(str)
rmdir(str,'s')
rehash
end
mkdir(str);
A = rand(10);
save(fullfile(str,'A'),'A')
[~,s] = fileattrib(str);
fprintf('UserWrite %d\n',s.UserWrite);
try
rmdir(str,'s');
rehash
catch mex
disp(mex)
throw(mex)
end
fprintf('OK %d\n',i);
end
Although it's quite random, usually it fails within 10 trials.
UserWrite 1
OK 1
UserWrite 1
OK 2
UserWrite 1
MException with properties:
identifier: 'MATLAB:RMDIR:SomeDirectoriesNotRemoved'
message: 'D:\xxxxx\TEMP_FOLDER could not be removed.'
cause: {0x1 cell}
stack: [0x1 struct]
D:\xxxxx\TEMP_FOLDER could not be removed.
Does anyone know how to solve this? I tested on Windows 7 (R2016a); the same code worked on OS X (R2016a).
  2 comentarios
Walter Roberson
Walter Roberson el 1 de Jun. de 2016
As a data point: on R2016a on OS-X, none of the rmdir fail.
Kouichi C. Nakamura
Kouichi C. Nakamura el 2 de Jun. de 2016
Yes, I tried myself too, and the test code worked fine on R2016a on OS X.

Iniciar sesión para comentar.

Respuesta aceptada

Philip Borghesani
Philip Borghesani el 2 de Jun. de 2016
This is often caused by interaction with your virus scanner that happens to still be scanning / looking into files that may already have been deleted in the directory. Short of disabling your virus scanner the only solution is wait a short time and retry the operation. I suggest using the status outputs from rmdir to write a short loop something like this: (untested)
for try=1:4
status=rmdir(str,s);
if status==1
break
end
sleep(.1*try)
end
if status==0
warning('mytest:leakedDir','Warning failed to clean up directory %s", str);
end
  4 comentarios
Kouichi C. Nakamura
Kouichi C. Nakamura el 3 de Jun. de 2016
Editada: Kouichi C. Nakamura el 3 de Jun. de 2016
Thanks a lot. Your report made me rethink of the culprit and I think I just found one! It's Dropbox. I put everything in Dropbox and it has been interfering file removal. When I paused Dropbox, I got no errors with the code at the top. But when I resumed Dropbox, I got loads of errors. And that was reproducible. Again Thank you for your input.
Volodymyr Zenin
Volodymyr Zenin el 10 de Mzo. de 2020
Thank you, Philip Borghesani, for giving this solution - I had the same problem as the Kouichi C. Nakamura, and I suspect OneDrive in interference... By the way, please correct your answer: 1) try cannot be used as variable - it is kind of function in new Matlab; 2) there is no more such function as sleep, one should use pause instead.

Iniciar sesión para comentar.

Más respuestas (1)

Andy Campbell
Andy Campbell el 2 de Jun. de 2016
Have you tried using the TemporaryFolderFixture or the WorkingFolderFixture?
  1 comentario
Kouichi C. Nakamura
Kouichi C. Nakamura el 2 de Jun. de 2016
Editada: Kouichi C. Nakamura el 2 de Jun. de 2016
Wow, I did not know there are specific types of fixture already available. Good to know, thanks!
Documentation says
Before it deletes the folder, the fixture clears from memory the definitions of any MATLAB-files, P-files, and MEX-files that are defined in the temporary folder.
Yes, this sounds like related to my issue.

Iniciar sesión para comentar.

Categorías

Más información sobre Search Path en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by