c0000005 Access_Violation calling Process.Start when running app in remote powershell session

0

I have a scenario where I need to execute some code through a remote powershell session, the code internally calls Process.Start() passing in (using ProcessStartInfo).

This seems to work, at least no error is returned from the call. But later the Exited event is raised on the process and the ExitCode is 0xc0000007.

If I run the same code under the same user credentials directly on the same machine everything runs fine, So I am am lead to the conclusion that it is the remote session that is the cause of the problems.

My question is how best to diagnose the root cause of the Access_Violation in these circumstances.

I have tried monitoring the process with ProcMon to see if there is an external resource is cannot access due to path or access rights. This did not help.

The actual code I am running is:

this.debug = new StringBuilder();
this.debug.AppendLine("trying to start driver service");
this.driverServiceProcess = new Process();
if (this.user != null)
{
   this.driverServiceProcess.StartInfo.UserName = user.Name;
   this.driverServiceProcess.StartInfo.Password = user.Password;
   this.driverServiceProcess.StartInfo.Domain = user.Domain;
}
this.driverServiceProcess.StartInfo.FileName = Path.Combine(this.driverServicePath, this.driverServiceExecutableName);
this.driverServiceProcess.StartInfo.Arguments = this.CommandLineArguments;
this.driverServiceProcess.StartInfo.UseShellExecute = false;
this.driverServiceProcess.StartInfo.CreateNoWindow = this.hideCommandPromptWindow;
this.driverServiceProcess.StartInfo.LoadUserProfile = true;
this.driverServiceProcess.Exited += (s, e) =>
{
    this.debug.AppendFormat("process exitted at '{0}'{1}", this.driverServiceProcess?.ExitTime, Environment.NewLine);
    this.debug.AppendFormat("with code '{0}'{1}", this.driverServiceProcess?.ExitCode, Environment.NewLine);
 };
 var started = this.driverServiceProcess.Start();
 this.debug.AppendLine("after start call, but before wait, process is " + (this.driverServiceProcess.Responding ? string.Empty : "not ") + "responding and started = " + started );
 bool serviceAvailable = this.WaitForServiceInitialization();

 if (!serviceAvailable)
 {
     string msg = "Cannot start the driver service on '" + this.ServiceUrl + "' {" + this.debug.ToString() + "}";
     throw new WebDriverException(msg);
 }

where the exe in question is the IEDriverService from Selenium and the WaitForServiceInitialization() method fires a web request to the driver service and expects a happy response:

try
{
    Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
    HttpWebRequest request = HttpWebRequest.Create(serviceHealthUri) as HttpWebRequest;
    request.KeepAlive = false;
    request.Timeout = 5000;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

    // Checking the response from the 'status' end point. Note that we are simply checking
    // that the HTTP status returned is a 200 status, and that the resposne has the correct
    // Content-Type header. A more sophisticated check would parse the JSON response and
    // validate its values. At the moment we do not do this more sophisticated check.
    this.debug.AppendLine("status returned from initialized check is " + response.StatusCode);
    isInitialized = response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
    response.Close();
}
catch (WebException ex)
{
    this.debug.AppendLine(DateTime.Now + "web exception in isinitialized: " + ex.Message);
    this.debug.Append(DateTime.Now + ex.InnerException.Message);
}

This is not received as the port is not open and listening by the time the request is sent, and an exception is encountered:

Cannot start the driver service on 'http://localhost:25531/' {
    trying to start driver service
    after start call, but before wait, process is responding
    waiting for service to initialise
    web exception in isinitialized: Unable to connect to the remote server
    No connection could be made because the target machine actively refused it 127.0.0.1:25531
    process exitted at '1/31/2017 12:21:00 PM'
    with code '-1073741819'at 1/31/2017 12:21:01 PM 
    service not running, returning from initialised check early
    wait for initialized returning False
}

Information from event log is:

Faulting application name: IEDriverServer.exe, version: 3.0.0.0, time stamp: 0x57ffc8fb
Faulting module name: KERNEL32.DLL_unloaded, version: 6.3.9600.17415, time stamp: 0x545049be
Exception code: 0xc0000005
Fault offset: 0x0001a720
Faulting process ID: 0x78f8
Faulting application start time: 0x01d27c828a7897b0
Faulting application path: C:\...\IEDriverServer.exe
Faulting module path: KERNEL32.DLL
Report ID: c86215a1-e875-11e6-82d4-ecb1d72e6816
Faulting package full name: 
Faulting package-relative application ID: 

and just before that:

Faulting application name: conhost.exe, version: 6.3.9600.17415, time stamp: 0x5450410b
Faulting module name: USER32.dll, version: 6.3.9600.18438, time stamp: 0x57ae642e
Exception code: 0xc0000142
Fault offset: 0x00000000000ecdd0
Faulting process ID: 0x8158
Faulting application start time: 0x01d27c828a7d8ce2
Faulting application path: C:\WINDOWS\system32\conhost.exe
Faulting module path: USER32.dll
Report ID: c835d4bf-e875-11e6-82d4-ecb1d72e6816
Faulting package full name: 
Faulting package-relative application ID: 
c#
powershell
debugging
remote-access
asked on Stack Overflow Feb 1, 2017 by MikeW • edited Feb 1, 2017 by MikeW

1 Answer

0

This was a problem of the way a process launch is performed when the session does not have a desktop to speak of.

Very similar to the problem described here Starting a process with credentials from a Windows Service which lead me to the solution using the code in the answer with a "compact" standalone code

answered on Stack Overflow Feb 1, 2017 by MikeW • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0