classification
Title: subprocess.Popen.terminate() inconsistent behavior on Windows
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brian.curtin, dabrahams, gregory.p.smith, loewis, pitrou, python-dev, tim.golden
Priority: normal Keywords: patch

Created on 2012-03-11 16:02 by dabrahams, last changed 2012-03-11 18:45 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
winspterminate.patch pitrou, 2012-03-11 16:42 review
Messages (8)
msg155391 - (view) Author: Dave Abrahams (dabrahams) Date: 2012-03-11 16:02
Try the following script on posix and Windows.  On Posix:

launched
. . . exiting
killed

on Windows:

launched
. . . exiting
Traceback (most recent call last):
  File "sp.py", line 16, in <module>
    p.terminate()
  File "c:\Python26\lib\subprocess.py", line 949, in terminate
    _subprocess.TerminateProcess(self._handle, 1)
WindowsError: [Error 5] Access is denied

This inconsistency seems unnecessary and is an obstacle to writing portable code.

from subprocess import *
import sys, time

p = Popen([sys.executable, '-c', '''
import time, sys
for i in range(3): 
    time.sleep(.3)
    print '.',
    sys.stdout.flush()
print 'exiting'
'''], stdout = sys.stdout, stderr = sys.stderr)

print 'launched'
time.sleep(2)

p.terminate()
print 'killed'
msg155392 - (view) Author: Dave Abrahams (dabrahams) Date: 2012-03-11 16:08
By the way, the suggested fix would be for terminate() to return a value indicating if the process were already terminated, and not throw an exception in that case.  For a user to handle the issue correctly on Windows is rather a nasty project involving a race between process death and the call to terminate()
msg155394 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-11 16:42
Here is a patch for 3.3. I'm not sure it should be backported, as it's a slight change in behaviour.
msg155396 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2012-03-11 17:11
Raising an exception on terminate is a bug.  I'd backport this to 2.7 and 3.2.  I don't actually have Windows to test on so i'll leave committing that to people who do.
msg155399 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-03-11 18:11
+1 for backporting to the bug fix releases.
msg155404 - (view) Author: Roundup Robot (python-dev) Date: 2012-03-11 18:37
New changeset 41b1fe5a75a6 by Antoine Pitrou in branch '3.2':
Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
http://hg.python.org/cpython/rev/41b1fe5a75a6

New changeset f452d7d5470d by Antoine Pitrou in branch 'default':
Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
http://hg.python.org/cpython/rev/f452d7d5470d
msg155405 - (view) Author: Roundup Robot (python-dev) Date: 2012-03-11 18:45
New changeset b6ec3b717f7e by Antoine Pitrou in branch '2.7':
Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.
http://hg.python.org/cpython/rev/b6ec3b717f7e
msg155406 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-11 18:45
Ok, I've then fixed the bug in all 3 branches.
History
Date User Action Args
2012-03-11 18:45:47pitrousetstatus: open -> closed
resolution: fixed
messages: + msg155406

stage: patch review -> resolved
2012-03-11 18:45:19python-devsetmessages: + msg155405
2012-03-11 18:37:53python-devsetnosy: + python-dev
messages: + msg155404
2012-03-11 18:11:18loewissetnosy: + loewis

messages: + msg155399
versions: + Python 2.7, Python 3.2
2012-03-11 17:11:44gregory.p.smithsetmessages: + msg155396
2012-03-11 16:42:07pitrousetfiles: + winspterminate.patch

versions: + Python 3.3, - Python 2.6
keywords: + patch
nosy: + gregory.p.smith, tim.golden, brian.curtin, pitrou

messages: + msg155394
stage: patch review
2012-03-11 16:08:04dabrahamssetmessages: + msg155392
2012-03-11 16:02:48dabrahamscreate