From: <pj...@us...> - 2008-10-25 23:03:44
|
Revision: 5508 http://jython.svn.sourceforge.net/jython/?rev=5508&view=rev Author: pjenvey Date: 2008-10-25 23:03:38 +0000 (Sat, 25 Oct 2008) Log Message: ----------- on SystemRestart: o shutdown all active sockets o hide InterruptedExceptions in threads for better SystemRestart support in PasteScript Modified Paths: -------------- trunk/jython/Lib/socket.py trunk/jython/Lib/threading.py trunk/jython/src/org/python/util/jython.java Modified: trunk/jython/Lib/socket.py =================================================================== --- trunk/jython/Lib/socket.py 2008-10-25 21:53:49 UTC (rev 5507) +++ trunk/jython/Lib/socket.py 2008-10-25 23:03:38 UTC (rev 5508) @@ -988,6 +988,16 @@ send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy __getattr__ = _dummy +_active_sockets = set() + +def _closeActiveSockets(): + for socket in _active_sockets.copy(): + try: + socket.close() + except error: + msg = 'Problem closing socket: %s: %r' % (socket, sys.exc_info()) + print >> sys.stderr, msg + class _socketobject(object): __doc__ = _realsocket.__doc__ @@ -1005,8 +1015,13 @@ meth = getattr(_sock, method, None) if meth: setattr(self, method, meth) + _active_sockets.add(self) def close(self): + try: + _active_sockets.remove(self) + except KeyError: + pass _sock = self._sock if isinstance(_sock, _nonblocking_api_mixin): _sock.close_lock.acquire() Modified: trunk/jython/Lib/threading.py =================================================================== --- trunk/jython/Lib/threading.py 2008-10-25 21:53:49 UTC (rev 5507) +++ trunk/jython/Lib/threading.py 2008-10-25 23:03:38 UTC (rev 5508) @@ -1,6 +1,8 @@ +from java.lang import InterruptedException from java.util import Collections, WeakHashMap from java.util.concurrent import Semaphore, CyclicBarrier from java.util.concurrent.locks import ReentrantLock +from org.python.util import jython from thread import _newFunctionThread from thread import _local as local import java.lang.Thread @@ -245,6 +247,11 @@ self.run() except SystemExit: pass + except InterruptedException: + # Quiet InterruptedExceptions if they're caused by + # _systemrestart + if not jython.shouldRestart: + raise except: # If sys.stderr is no more (most likely from interpreter # shutdown) use self.__stderr. Otherwise still use sys (as in Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2008-10-25 21:53:49 UTC (rev 5507) +++ trunk/jython/src/org/python/util/jython.java 2008-10-25 23:03:38 UTC (rev 5508) @@ -63,7 +63,7 @@ "JYTHONPATH: '" + java.io.File.pathSeparator + "'-separated list of directories prefixed to the default module\n" + " search path. The result is sys.path."; - private static boolean shouldRestart; + public static boolean shouldRestart; public static void runJar(String filename) { // TBD: this is kind of gross because a local called `zipfile' just @@ -228,12 +228,12 @@ } catch(Throwable t) { if (t instanceof PyException && Py.matchException((PyException)t, _systemrestart.SystemRestart)) { - // Stop current threads... - thread.interruptAllThreads(); + // Shutdown this instance... + shouldRestart = true; + shutdownInterpreter(); // ..reset the state... Py.setSystemState(new PySystemState()); // ...and start again - shouldRestart = true; } else { Py.printException(t); if (!opts.interactive) { @@ -323,6 +323,21 @@ //System.err.println("imp"); return interp; } + + /** + * Run any finalizations on the current interpreter in preparation for a SytemRestart. + */ + public static void shutdownInterpreter() { + // Stop all the active threads + thread.interruptAllThreads(); + // Close all sockets -- not all of their operations are stopped by + // Thread.interrupt (in particular pre-nio sockets) + try { + imp.load("socket").__findattr__("_closeActiveSockets").__call__(); + } catch (PyException pye) { + // continue + } + } } class CommandLineOptions This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |