kill doesn"t work correctly

kill doesn"t work correctly

am 05.04.2010 10:25:40 von Octavian Rasnita

Hi,

C:\Documents and Settings\Octavian>taskkill /pid 1980
ERROR: The process "1980" not found.
C:\Documents and Settings\Octavian>perl -v
This is perl, v5.10.1 built for MSWin32-x86-multi-thread
(with 2 registered patches, see perl -V for more detail)
C:\Documents and Settings\Octavian>perl -e "print kill(0, 1980);"
1

So the process with the PID 1980 doesn't exist, but kill(0) reports that it exists.
Am I doing something wrong or there is a bug in this version of Perl?

Thanks.

Octavian

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: kill doesn"t work correctly

am 06.04.2010 00:21:58 von Jan Dubois

On Mon, 05 Apr 2010, Octavian Rasnita wrote:
>
> C:\Documents and Settings\Octavian>taskkill /pid 1980
> ERROR: The process "1980" not found.
> C:\Documents and Settings\Octavian>perl -v
> This is perl, v5.10.1 built for MSWin32-x86-multi-thread
> (with 2 registered patches, see perl -V for more detail)
> C:\Documents and Settings\Octavian>perl -e "print kill(0, 1980);"
> 1
>
> So the process with the PID 1980 doesn't exist, but kill(0) reports that it exists.
> Am I doing something wrong or there is a bug in this version of Perl?

I can't reproduce the problem with either build 1006 nor 1007
(the only 5.10.1 builds of ActivePerl released so far), so I have
no idea why this is failing for you.

I checked both Windows XP and Windows 7, FWIW.

The only 2 explanations I can come up with are:

1) The perl process above gets the pid 1980. Windows reuses pids quite rapidly,
so you should print out $$ in your sample script to make sure you are not
using this supposedly non-existing pid.

2) Something else spawns a new process between the time you run taskkill.exe
and perl.exe, and that new process gets pid 1980.

kill(0, $pid) on Perl essentially just calls

handle = OpenProcess(PROCESS_TERMINATE, FALSE, pid);

and if the handle is not NULL then it returns '1', otherwise it returns
'0'. I don't see how OpenProcess() could return a valid process handle
if the PID is not valid at the time of the call. The OpenProcess
documentation is here:

http://msdn.microsoft.com/en-us/library/ms684320%28VS.85%29. aspx

Cheers,
-Jan


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: kill doesn"t work correctly

am 06.04.2010 00:44:20 von Bill Luebkert

Jan Dubois wrote:
>
> I can't reproduce the problem with either build 1006 nor 1007
> (the only 5.10.1 builds of ActivePerl released so far), so I have
> no idea why this is failing for you.
>
> I checked both Windows XP and Windows 7, FWIW.

I can reproduce it in 5.8 B820. I run a simple looping perl script that
prints out the pid in one window and try to kill it from another with
the same script (using argv to differentiate which function):

use strict;
use warnings;

# sleep if no argv

if (not @ARGV) {
print "pid=$$ - going into sleep loop\n";
while (1) { sleep 5; }
exit;
}

# else try to kill argv (signal and PID as args)

(my $sig, $pid) = @ARGV;

print "Attempting to kill $sig, $pid\n";
my $ret = kill $sig, $pid;
print "kill returned $ret\n";
exit;

__END__

Window 1:

<24> p xx.pl
pid=6508 - going into sleep loop

Window 2:

<472> p xx.pl 0 6508
Attempting to kill 0, 6508 Attempt to test if 6508 is there - good return
kill returned 1

<473> p xx.pl 0 6509 Attempt to kill non-existent 6509 (checked taskmaster) and get a good return
Attempting to kill 0, 6509
kill returned 1

<474> p xx.pl 9 6509 Killed 6508 using 6509 as PID and sig of 9 ???
Attempting to kill 9, 6509
kill returned 1

<475> p xx.pl 9 6509 Here we get a proper 0 return when repeating
Attempting to kill 9, 6509
kill returned 0

Strange stuff.

I get similar weird returns from OpenProcess using API.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: kill doesn"t work correctly

am 06.04.2010 01:04:16 von Jan Dubois

On Mon, 05 Apr 2010, Bill Luebkert wrote:
> Jan Dubois wrote:
> >
> > I can't reproduce the problem with either build 1006 nor 1007
> > (the only 5.10.1 builds of ActivePerl released so far), so I have
> > no idea why this is failing for you.
> >
> > I checked both Windows XP and Windows 7, FWIW.
>
> I can reproduce it in 5.8 B820.

Yes, I can do that too, but it is not very interesting as the Changelog
already explains that kill(0, $pid) was broken in 820 and fixed in 822
(821 was an HP-UX specific build):

http://docs.activestate.com/activeperl/5.8/changes.html#buil d_822_wednesday__aug_1__2007

If you dig into the bug report, you'll see that it was also broken in build 5.10.0.1000
(which was just a beta release), but should have been fixed in build 1001:

http://bugs.activestate.com/show_bug.cgi?id=67519

Octavian explicitly mentioned that he was using Perl 5.10.1,
which means build 1006 or 1007.

[...]

> I get similar weird returns from OpenProcess using API.

Could you elaborate on that?

Cheers,
-Jan


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: kill doesn"t work correctly

am 06.04.2010 01:50:49 von Jan Dubois

On Mon, 05 Apr 2010, Jan Dubois wrote:
> On Mon, 05 Apr 2010, Bill Luebkert wrote:
> > I can reproduce it in 5.8 B820.
>
> Yes, I can do that too, but it is not very interesting as the Changelog
> already explains that kill(0, $pid) was broken in 820 and fixed in 822
> (821 was an HP-UX specific build):
>
> http://docs.activestate.com/activeperl/5.8/changes.html#buil d_822_wednesday__aug_1__2007

Looking more closely shows that the bug fix got lost before the actual 822
release again. 823 was a non-public release, so kill(0, $pid) wasn't
really fixed until build 824.

> [...]
>
> > I get similar weird returns from OpenProcess using API.
>
> Could you elaborate on that?

Just to explain the problem in earlier Perl versions: the bug was
introduced by some refactoring to support kill(-$signal, $pid) on
Windows to send a signal too all processes in a process tree. That
resulted in checking the return value of OpenProcess() against
INVALID_HANDLE_VALUE, which is the normal Win32 in-band error value.

OpenProcess() however is not following that convention and returns
NULL as the error value (which technically is a valid HANDLE value,
except OpenProcess() cannot return the pid of the "System Idle
Process" pseudo-process anyways).

Cheers,
-Jan


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: kill doesn"t work correctly

am 06.04.2010 08:46:11 von Octavian Rasnita

Hi Jan,

I have tried for too many times with wrong results, so I doubt that exactly
that process ID gets used each time.

But I have tried the following program which always work:

use Win32::Process::Info;

my $pid = 3272;

my @pids = Win32::Process::Info->new->ListPids;

print grep($_ == $pid, @pids) ? 'yes' : 'no';

Thanks.

--
Octavian

----- Original Message -----
From: "Jan Dubois"
To: "'Octavian Rasnita'" ;

Sent: Tuesday, April 06, 2010 1:21 AM
Subject: RE: kill doesn't work correctly


> On Mon, 05 Apr 2010, Octavian Rasnita wrote:
>>
>> C:\Documents and Settings\Octavian>taskkill /pid 1980
>> ERROR: The process "1980" not found.
>> C:\Documents and Settings\Octavian>perl -v
>> This is perl, v5.10.1 built for MSWin32-x86-multi-thread
>> (with 2 registered patches, see perl -V for more detail)
>> C:\Documents and Settings\Octavian>perl -e "print kill(0, 1980);"
>> 1
>>
>> So the process with the PID 1980 doesn't exist, but kill(0) reports that
>> it exists.
>> Am I doing something wrong or there is a bug in this version of Perl?
>
> I can't reproduce the problem with either build 1006 nor 1007
> (the only 5.10.1 builds of ActivePerl released so far), so I have
> no idea why this is failing for you.
>
> I checked both Windows XP and Windows 7, FWIW.
>
> The only 2 explanations I can come up with are:
>
> 1) The perl process above gets the pid 1980. Windows reuses pids quite
> rapidly,
> so you should print out $$ in your sample script to make sure you are
> not
> using this supposedly non-existing pid.
>
> 2) Something else spawns a new process between the time you run
> taskkill.exe
> and perl.exe, and that new process gets pid 1980.
>
> kill(0, $pid) on Perl essentially just calls
>
> handle = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
>
> and if the handle is not NULL then it returns '1', otherwise it returns
> '0'. I don't see how OpenProcess() could return a valid process handle
> if the PID is not valid at the time of the call. The OpenProcess
> documentation is here:
>
> http://msdn.microsoft.com/en-us/library/ms684320%28VS.85%29. aspx
>
> Cheers,
> -Jan
>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 5002 (20100405) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>


__________ Information from ESET NOD32 Antivirus, version of virus signature database 5002 (20100405) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: kill doesn"t work correctly

am 06.04.2010 10:02:53 von Jan Dubois

On Mon, 05 Apr 2010, Octavian Rasnita wrote:
> I have tried for too many times with wrong results, so I doubt that exactly
> that process ID gets used each time.
>
> But I have tried the following program which always work:
>
> use Win32::Process::Info;
>
> my $pid = 3272;
>
> my @pids = Win32::Process::Info->new->ListPids;
>
> print grep($_ == $pid, @pids) ? 'yes' : 'no';

Not sure what you are trying to say here. Are you saying that
kill(0, $pid) will return '1' for *any* value of $pid?

If you want to check the PID against the current PID, please
use the $$ variable for the PID of the Perl process.

Beyond that you need to provide additional information, like
which version of Windows you are using; if you are running as
Administrator or not, etc...

It would also help if you could try to reproduce the problem
on a separate system, if you have access to more than one.

Cheers,
-Jan

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: kill doesn"t work correctly

am 06.04.2010 12:05:12 von Octavian Rasnita

From: "Jan Dubois"
> On Mon, 05 Apr 2010, Octavian Rasnita wrote:
>> I have tried for too many times with wrong results, so I doubt that
>> exactly
>> that process ID gets used each time.
>>
>> But I have tried the following program which always work:
>>
>> use Win32::Process::Info;
>>
>> my $pid = 3272;
>>
>> my @pids = Win32::Process::Info->new->ListPids;
>>
>> print grep($_ == $pid, @pids) ? 'yes' : 'no';
>
> Not sure what you are trying to say here. Are you saying that
> kill(0, $pid) will return '1' for *any* value of $pid?


No, it returns 1 for the processes that just ended (but a pretty long time
after those processes were killed).

I have made a program that runs for a longer time:

$|++;
print $$;
sleep 60;

Then I get the returned PID and I use it in the following program:

my $pid = 3764;
print kill(0, $pid);

While the first program is running, the second program prints 1. Right after
the first program ended, the second program continues to print 1 although
there is no process with that PID running (I tested this by using the
program I included in my previous message).

I have just tested this on another computer which also runs Windows XP Pro
(installed for only few days).
I run it with a username other than "Administrator" but with administrative
privileges.

Thanks.

Octavian


__________ Information from ESET NOD32 Antivirus, version of virus signature database 5003 (20100406) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: kill doesn"t work correctly

am 06.04.2010 20:04:39 von Jan Dubois

On Tue, 06 Apr 2010, Octavian Rasnita wrote:
> No, it returns 1 for the processes that just ended (but a pretty long
> time after those processes were killed).

Note that there is no guarantee that the PID will become invalid just
because the process enters the INACTIVE stage. The PID will remain
valid until *all* handles to this process have been closed. So it
is not enough to just kill the process; whatever parent has started
it must make sure it hasn't kept any references to it either. So
how exactly are you starting the process?

If you look at the POSIX definition of kill() you'll read that it
explicitly mentions that process lifetime covers the inactive
phase as well:

| In particular, this means that an application cannot have a parent
| process check for termination of a particular child with kill().

You can use Win32::Process to check if the process is still running
or not though (warning, untested code):

require Win32::Process;
Win32::Process::Open(my $process, $pid, 0);
Win32::Process::GetExitCode(my $exitcode);
if ($exitcode == Win32::Process::STILL_ACTIVE()) {
# still running
}
else {
# zombie waiting to be reaped
}

Cheers,
-Jan


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs