0

I have a windows service run with local system account

enter image description here

What my program does is :

if (File.Exists(outputPath))
{
    File.Delete(outputPath);
}
File.Move(archivePath, outputPath);

Relevant folder is application folder of iis where its application pool's identity is ApplicationPoolIdentity located under c:\MyAppFolder.

My windows service does its few times a day, and my clients checks if any new version exists every 5 minutes(0,5,10,15...) and download that file.

Time to time, file is somehow get "locked" on filesystem then

  • iis gives 401 error
  • File cannot be deleted

My first question how can I repro this situation?

One patch is done by colleagues is:

var fs = File.GetAccessControl(outputPath);
fs.SetAccessRuleProtection(false, false);
File.SetAccessControl(outputPath, fs);

Although this patch, it seems error occured again,

I may apply, this solution as well.

Are those solutions are enough or necessary?

Again my first question is important "repro issue" and understand why this happens.

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
  • 1
    The client is probably not doing the right thing or your service is not, or you have some other application holding the file open like a virus checker or something, also which file is getting access denied, `archivePath` or `outputPath` – TheGeneral Jul 02 '18 at 08:39
  • @TheGeneral I thinks it is not the issue and it doesn't happens constantly, – asdf_enel_hak Jul 02 '18 at 08:41
  • windows doesn't just lock files for the fun of it, neither does .net, and i seriously doubt there is a bug in `File.Delete` or `File.Move` – TheGeneral Jul 02 '18 at 08:42
  • any chance the anti virus is still scanning the file – BugFinder Jul 02 '18 at 08:42
  • @BugFinder "let me check on prod server, just in case", yes there is a anti virus prog, so then let me repro issue with scanning relevant file. Thanks – asdf_enel_hak Jul 02 '18 at 08:43
  • 1
    I have had antivirus blocking files like this, but I've also seen some unexpected cases dues to memory leaks in GDI. I used [handle](https://learn.microsoft.com/en-us/sysinternals/downloads/handle) to get some hard facts in production on who is blocking (was never able to repro on test envs before knowing what it was). You need to call handle right away in your try-catch. – Tewr Jul 02 '18 at 09:06
  • @Tewr Process explorer will help a lot. Thanks – asdf_enel_hak Jul 02 '18 at 11:01
  • Process explorer might not be fast enough. Depends on your problem. What I did was `try {do(file)} catch(Exception e) { ExecuteHandleExe(file) }` – Tewr Jul 02 '18 at 11:42
  • @Tewr Indeed, it will be ok, because once file is locked, it is permenant, until I stop-start application pool – asdf_enel_hak Jul 02 '18 at 11:43
  • oh. Then Process explorer/handle will just return IIS (w3wp.exe), which probably wont help you. Are you serving the file at Outputpath statically with your iis application? – Tewr Jul 02 '18 at 11:48
  • @Tewr At least I can verify if the antivirus prog intervenes – asdf_enel_hak Jul 02 '18 at 13:23

0 Answers0