none
Process.Start fails (sometimes) with access denied RRS feed

  • Question

  • From a webservice i do a virusscan on uploaded file using a command line utility of the virus scanner in use.

    It is possible to upload multiple files in one request and the files will be scanned one-by-one, sequentially.

    The code i am using is the following:

    using (Process process = new Process
    {
      StartInfo = new ProcessStartInfo(SCANEXE)
      {
        Arguments = $"\"{filePath}\" /some-switch /another-switch",
        CreateNoWindow = true,
        ErrorDialog = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        UseShellExecute = false
      }
    })
    {
      process.Start();
      if (process.WaitForExit(timeoutInMs))
        process.WaitForExit();
    
      if (process.HasExited)
      {
        switch (process.ExitCode)
        {
          case 0:
            return new ScanResult
            {
              ExitCode = process.ExitCode,
              State = ScanResult.States.FILEISCLEAN
    	};
          case 1:
          case 50:
            return new ScanResult
            {
              ExitCode = process.ExitCode,
              State = ScanResult.States.FILEISDIRTY
            };
          case 10:
          case 100:
            return new ScanResult
            {
              ExitCode = process.ExitCode,
              State = ScanResult.States.SCANERROR
            };
          default:
            return new ScanResult
            {
              ExitCode = process.ExitCode,
              State = ScanResult.States.SCANERROR
            };
        }
      }
      process.Kill();
      return new ScanResult
      {
        State = ScanResult.States.SCANTIMEOUT
      };
    }

    What happens is that in approximately 5% of all scan attempts, Process.Start fails with an "Access Denied". The behavior is completely random, and has nothing to do with file beeing scanned. The same file will be scanned correctly in a subsequent try.

    Wrapping the proces in a using statement as shown above (it was not, originally) improves the situation but does not completely fix the issue. (from 5% to approx. 1%)

    It appeared to me like a timing issue so I did put in a Thread.Sleep(100) to get some time between process ending and next process starting. Without any success...

    Does somebody have any clue ?

    I am running out of ideas here...

    Maybe worth noticing:

    This problem magically started 1 day after a in-place-upgrade from the server from W2008 to W2012.


    • Edited by everttimmer Thursday, January 23, 2020 4:06 PM
    Thursday, January 23, 2020 4:04 PM

All replies

  • Is there any chance that two calls into the webservice might occur at the same time? If that happened, then you would launch two scanning processes at the same time and there could be some conflicts when both tried to access the same file.
    Thursday, January 23, 2020 4:13 PM
    Moderator
  • Hello,

    Have you looked at the log files on the server and event logs?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 23, 2020 5:12 PM
    Moderator
  • No eventlog entries and the exception lists:

    Access is denied

        at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
        at System.Diagnostics.Process.Start()
        at blablabla.IsFileClean(string filePath, Int32 timeOutMs)

    The original code swallowed all exceptions and reported "Possible virus in file x" if anything in the scanning process went south so we got this exception from a command line utility that gave us more info on the problem.

    Thursday, January 23, 2020 8:04 PM
  • I had a look at the code in the webservice.

    The component that manages the scan is not threadsafe but i do not suspect it to be the cause of this issue cause the same problem occurred when running the component from a command line utility that mimics the way the component is invoked. That command line uitility is on a single thread so threading can't be the problem.

    The way the component is used is that the webservice provides it with a byte array. The component then creates a temporary file by using Path.GetTempfileName() and writes the byte array to it. Finally, the component calls the command line scan utility to scan the temporary file.

    A filename clash seems not to be possible this way, i think?

    What I need to check tomorrow, is, if the scan utility allows multiple instances to be started. If that is not the case, two simultanuous calls coult be the cause (thanks for the tip). Running the command line tool only produces the same problem on the production server and that is... where the service is running... Multiple simultanuous processes can be the cause then...

    Thursday, January 23, 2020 8:19 PM