Overview
Features
Download
Documentation
Community
Add-Ons & Services

Process::kill

Please post support and help requests here.

Process::kill

Postby BigTick » 29 Apr 2013, 22:08

Hi,

I have the following situation: I create a process and want to read its output, but kill it if it doesn't terminate within 20s.

I have one thread that creates the process with:
Code: Select all
PipeInputStream istr(outPipe)
ProcessHandle phdl = Poco::Process::launch(path, args, 0, &outPipe, 0);
try {
   istr >> results;
}


Code: Select all
and another one that does:
if (!sleep(20*1000)) {
    Poco::Process::kill(*_processHandle);
}


It all works fine, except that when Poco::Process::kill is killed, it calls CloseHandle(_hProcess);
When phdl gets out of scope, it also calls CloseHandle(), thus trying to close a handle that's already been closed (and potentially reallocated to some other process).

Am I missing something in the logic here ?

Many thanks in advance,
P.
BigTick
 
Posts: 35
Joined: 28 Apr 2010, 21:07

Re: Process::kill

Postby alex » 30 Apr 2013, 00:30

BigTick wrote:Am I missing something in the logic here ?


Probably not. It would be best to file a bug on Github (a pull request even better).
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Process::kill

Postby BigTick » 30 Apr 2013, 16:38

Hmmm, ok, I've fixed it but it requires quite a few changes (some API's have a const ProcessHandleImpl& and need to be changed to remove the const).
I'll send it to Guenther through github.

Ideally what would be required is a method
int ProcessHandle::wait(int milliseconds)

that waits for a process completion, or for a timeout. This is straightforward to implement on Windows, but apparently very tricky on other OS'es.
BigTick
 
Posts: 35
Joined: 28 Apr 2010, 21:07

Re: Process::kill

Postby alex » 30 Apr 2013, 17:02

I thought a bool flag would do (set it to true in killImpl() and then check in destructor whether CloseHandle call is needed), but I did not spend much time looking at it and I'm probably missing something.
EDIT: maybe even better: set handle to 0;

In any case, if possible I'd break it down into two changes: (1) the simple one I described here (that would go into 1.5.2) release and (2) the more comprehensive fix proposed by you above.
alex
 
Posts: 1146
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Process::kill

Postby BigTick » 30 Apr 2013, 19:21

I did the "set handle to 0" thing. This is really the most sensible way to do it.
Unfortunately, this means the parameters and methods need to lose their const attribute.
BigTick
 
Posts: 35
Joined: 28 Apr 2010, 21:07


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest