3

I am invoking a command line process from C# that does an update from some remote server. Works fine when it can find the remote server and fetch data from it. Also works fine if not connected to a network.

However, when the remote server is unreachable, the external process will try to fetch data indefinitely, and there is no command line options to specify a timeout. So I added a force kill on the process after trying for 15 seconds.

using (var process = new Process {...})
{
    process.Start();
    if (!process.WaitForExit(15 * 1000))
    {
        process.Kill();
    }
}

I can see the process being terminated when process.Kill() is invoked, but then it hangs when exiting the using scope (i.e. when disposing the process).

What's wrong with this? Am I missing something to make sure this process is properly killed?

Thanks!

EDIT:

Process StartInfo is

FileName = command,
Arguments = arguments,
WorkingDirectory = workDir,
CreateNoWindow = true,
UseShellExecute = false
Erunehtar
  • 1,583
  • 1
  • 19
  • 38
  • 2
    I think you're going to need to provide some more details about the how you're creating the process, since the code above seems to work just fine for me. It seems likely that something in the code you've *not* provided (perhaps the `{...}`) may be to blame. – Iridium May 28 '18 at 20:11
  • @Iridium added process `StartInfo` that I use. – Erunehtar May 28 '18 at 20:19
  • I ran into a similar problem years ago where StdInput and StdOutput were waiting for each other and the process deadlocks. Consult [Why does StandardOutput.Read block](https://stackoverflow.com/questions/6655613/)? – Dour High Arch May 28 '18 at 20:25
  • @DourHighArch, of course stdout blocks, but it should definitely stop blocking and return EOF when the process dies. – glenebob May 28 '18 at 20:31
  • And this works for me, with cmd.exe, using OP's code. The process dies, and `.Dispose()` returns immediately. That is, if I add `process.Start()`, so I suspect there is other code still missing. – glenebob May 28 '18 at 20:32
  • This issue specifically happens only on a process that connects to a remote server. Does not happen if the process doesn't use network. – Erunehtar May 28 '18 at 20:47
  • @glenebob sorry I forgot about the `process.Start()` and the read begin on the streams. – Erunehtar May 28 '18 at 20:50
  • 1
    Still works for me. Even if I don't kill the process, `Dispose()` returns immediately. Very strange that network activity in the child process should have any effect within the parent. – glenebob May 28 '18 at 20:59
  • Are you subscribing to any events on the Process? You must be to use BeginOutputReadLine properly. If you aren't showing us the event subscriptions, please show those. Are you signing up for the Exited event too? – Mike Zboray May 28 '18 at 21:11
  • Nothing special about the output/error callbacks registration; they are empty. So I went ahead and removed them in the code, updated the OP accordingly. – Erunehtar May 29 '18 at 13:16
  • 1
    After searching for a long time, finally found something that seems related to my issue: https://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open. Turns out if I set `UseShellExecute` to `true`, the issue goes away! – Erunehtar May 29 '18 at 13:22
  • 1
    Reading more on the topic, it looks like I hit the issue where the child process inherits handles. And unfortunately there is no easy and portable way to fix it. In Core, it’s still open for debate: https://github.com/dotnet/corefx/issues/306 – Erunehtar May 29 '18 at 13:39
  • 1
    Possible duplicate of [Socket doesn't close after application exits if a launched process is open](https://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open) – Erunehtar May 29 '18 at 13:56
  • @Deathicon, same scenario happens, when output streams are redirected. It causes child process inherits them &, so holds them, while is alive itself. Leaving the using block causes an attempt to release streams, which are holding by child process. – user1234567 Jan 29 '21 at 12:16

0 Answers0