Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ef905d

Browse files
committed
Issue #12540: Prevent zombie IDLE processes on Windows due to changes
in os.kill(). Original patch by Eli Bendersky.
2 parents 252365b + e5cad23 commit 2ef905d

2 files changed

Lines changed: 18 additions & 26 deletions

File tree

Lib/idlelib/PyShell.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import threading
1111
import traceback
1212
import types
13+
import subprocess
1314

1415
import linecache
1516
from code import InteractiveInterpreter
@@ -37,11 +38,6 @@
3738
HOST = '127.0.0.1' # python execution server on localhost loopback
3839
PORT = 0 # someday pass in host, port for remote debug capability
3940

40-
try:
41-
from signal import SIGTERM
42-
except ImportError:
43-
SIGTERM = 15
44-
4541
# Override warnings module to write to warning_stream. Initialize to send IDLE
4642
# internal warnings to the console. ScriptBinding.check_syntax() will
4743
# temporarily redirect the stream to the shell window to display warnings when
@@ -344,13 +340,12 @@ def __init__(self, tkconsole):
344340
self.port = PORT
345341

346342
rpcclt = None
347-
rpcpid = None
343+
rpcsubproc = None
348344

349345
def spawn_subprocess(self):
350346
if self.subprocess_arglist is None:
351347
self.subprocess_arglist = self.build_subprocess_arglist()
352-
args = self.subprocess_arglist
353-
self.rpcpid = os.spawnv(os.P_NOWAIT, sys.executable, args)
348+
self.rpcsubproc = subprocess.Popen(self.subprocess_arglist)
354349

355350
def build_subprocess_arglist(self):
356351
assert (self.port!=0), (
@@ -365,12 +360,7 @@ def build_subprocess_arglist(self):
365360
command = "__import__('idlelib.run').run.main(%r)" % (del_exitf,)
366361
else:
367362
command = "__import__('run').main(%r)" % (del_exitf,)
368-
if sys.platform[:3] == 'win' and ' ' in sys.executable:
369-
# handle embedded space in path by quoting the argument
370-
decorated_exec = '"%s"' % sys.executable
371-
else:
372-
decorated_exec = sys.executable
373-
return [decorated_exec] + w + ["-c", command, str(self.port)]
363+
return [sys.executable] + w + ["-c", command, str(self.port)]
374364

375365
def start_subprocess(self):
376366
addr = (HOST, self.port)
@@ -428,7 +418,7 @@ def restart_subprocess(self):
428418
pass
429419
# Kill subprocess, spawn a new one, accept connection.
430420
self.rpcclt.close()
431-
self.unix_terminate()
421+
self.terminate_subprocess()
432422
console = self.tkconsole
433423
was_executing = console.executing
434424
console.executing = False
@@ -469,23 +459,22 @@ def kill_subprocess(self):
469459
self.rpcclt.close()
470460
except AttributeError: # no socket
471461
pass
472-
self.unix_terminate()
462+
self.terminate_subprocess()
473463
self.tkconsole.executing = False
474464
self.rpcclt = None
475465

476-
def unix_terminate(self):
477-
"UNIX: make sure subprocess is terminated and collect status"
478-
if hasattr(os, 'kill'):
466+
def terminate_subprocess(self):
467+
"Make sure subprocess is terminated"
468+
try:
469+
self.rpcsubproc.kill()
470+
except OSError:
471+
# process already terminated
472+
return
473+
else:
479474
try:
480-
os.kill(self.rpcpid, SIGTERM)
475+
self.rpcsubproc.wait()
481476
except OSError:
482-
# process already terminated:
483477
return
484-
else:
485-
try:
486-
os.waitpid(self.rpcpid, 0)
487-
except OSError:
488-
return
489478

490479
def transfer_path(self):
491480
self.runcommand("""if 1:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ Core and Builtins
249249
Library
250250
-------
251251

252+
- Issue #12540: Prevent zombie IDLE processes on Windows due to changes
253+
in os.kill().
254+
252255
- Issue #12683: urlparse updated to include svn as schemes that uses relative
253256
paths. (svn from 1.5 onwards support relative path).
254257

0 commit comments

Comments
 (0)