[Idle-dev] Fix for long delays

David Scherer dscherertemp at gmail.com
Thu May 15 00:15:10 CEST 2008


Hi all,

Here is a patch to the IDLE shipped with Python 2.5.2 that I regard as a bug
fix.  Specifically, it makes IDLE terminate its subprocess on Windows with
TerminateProcess (instead of just by closing a socket), and on UNIX with
SIGKILL instead of SIGTERM.  The Windows change is especially important,
because without it IDLE rather often fails to end my programs in a timely
fashion when I close the output window, restart the shell, or just press F5
to run the program again.  I think, however, that the behavior on all
platforms should be to terminate the user program immediately whether or not
it cooperates; that's what I expect when I give such a command to an IDE,
and a user program running disconnected from its output terminal is not a
good thing.

I'm working on Windows at the moment, so I'd greatly appreciate it if
someone on the list can test my changes on a platform that has os.kill.

I suspect that the code in run.py has some quirks that exacerbate the bad
behavior without the patch (for example, why the 10 second timeout in
MyHandler.exithook?), but my patch appears to make them irrelevant, as well
as deal with really nasty ways that the running program could freeze itself
up (e.g. a C extension that blocks without releasing the interpreter lock).
If there are any platforms other than Windows without os.kill, it might be
worth at least getting rid of the timeout.

Comments or questions?

Dave


--- c:\temp\Python-2.5.2\lib\idlelib\PyShell.py    2006-10-12
> 03:57:24.000000000 -0400
> +++ c:\python25\lib\idlelib\PyShell.py    2008-05-14 17:09:29.958875000
> -0400
> @@ -12,6 +12,7 @@
>  import traceback
>  import types
>  import macosxSupport
> +import subprocess
>
>  import linecache
>  from code import InteractiveInterpreter
> @@ -39,10 +40,31 @@
>  IDENTCHARS = string.ascii_letters + string.digits + "_"
>  LOCALHOST = '127.0.0.1'
>
> -try:
> -    from signal import SIGTERM
> -except ImportError:
> -    SIGTERM = 15
> +if hasattr(os, 'kill'):
> +    try:
> +        from signal import SIGKILL
> +    except ImportError:
> +        SIGTERM = 9
> +
> +    def kill( proc ):
> +        try:
> +            os.kill( proc.pid, SIGKILL )
> +        except OSError:
> +            # process already terminated:
> +            return
> +        else:
> +            try:
> +                proc.wait()
> +            except OSError:
> +                return
> +elif sys.platform[:3] == 'win':
> +    import ctypes
> +    def kill( proc ):
> +        ctypes.windll.kernel32.TerminateProcess( int(proc._handle), -1 )
> +        proc.wait()
> +else:
> +    def kill( proc ):
> +        pass # Hope the RPC mechanism will do the job
>
>  # Override warnings module to write to warning_stream.  Initialize to send
> IDLE
>  # internal warnings to the console.  ScriptBinding.check_syntax() will
> @@ -343,11 +365,11 @@
>
>      port = 8833
>      rpcclt = None
> -    rpcpid = None
> +    rpcproc = None
>
>      def spawn_subprocess(self):
>          args = self.subprocess_arglist
> -        self.rpcpid = os.spawnv(os.P_NOWAIT, sys.executable, args)
> +        self.rpcproc = subprocess.Popen( [sys.executable] + args[1:] )
>
>      def build_subprocess_arglist(self):
>          w = ['-W' + s for s in sys.warnoptions]
> @@ -463,17 +485,7 @@
>
>      def unix_terminate(self):
>          "UNIX: make sure subprocess is terminated and collect status"
> -        if hasattr(os, 'kill'):
> -            try:
> -                os.kill(self.rpcpid, SIGTERM)
> -            except OSError:
> -                # process already terminated:
> -                return
> -            else:
> -                try:
> -                    os.waitpid(self.rpcpid, 0)
> -                except OSError:
> -                    return
> +        kill( self.rpcproc )
>
>      def transfer_path(self):
>          self.runcommand("""if 1:
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/idle-dev/attachments/20080514/c702bd67/attachment.htm>


More information about the IDLE-dev mailing list