[2.2.1]How To Gracefully Shutdown an XML-RPC Server

T

The Jetman

I'm reading source code, but I haven't found any examples of
how to simply shutdown my XML-RPC server. I've done some nice things
w/ it so far. I can upload and dload arbitrary files, remotely execute
sys commands, etc. But I now want to use this code in a production
project and essentially control a FreeBSD box from a MSOFT Access
DB application. But the only Python fn I haven't been able to coerce
into submission is sys.exit() !

Here's my server method:

def sys_Shutdown( self ):
# don't work as is....
sys.exit( 0 )
return 0

And here's what happens:

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "D:\PYTHON22\lib\xmlrpclib.py", line 821, in __call__
return self.__send(self.__name, args)
File "D:\PYTHON22\lib\xmlrpclib.py", line 975, in __request
verbose=self.__verbose
File "D:\PYTHON22\lib\xmlrpclib.py", line 853, in request
return self.parse_response(h.getfile())
File "D:\PYTHON22\lib\xmlrpclib.py", line 896, in parse_response
return u.close()
File "D:\PYTHON22\lib\xmlrpclib.py", line 571, in close
raise apply(Fault, (), self._stack[0])
Fault: <Fault 1: 'exceptions.SystemExit:0'>

This is really bugging me, since I built everything else on
my own. Any ideas ? Jet
 
T

The Jetman

Andrew Dalke said:
The Jetman:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/114579

Or you can ungracefully use os._exit, like pulling the emergency
brake on a train.

Andrew
(e-mail address removed)

Andrew:

Thanx ! I *swear* I always spend at least a couple hours or so looking
for stuf like this on my own ! That's the single most frustrating part
about the Web (besides this idiotic W32.Swen virus/worm) ! No matter
how you compose a query, there's always something that you miss....

BTW, I haven't tried it, but I bet os._exit will fail just like
sys.exit(). Besides the reason for a graceful shutdown is more
than cosmetic, since a forced shutdown (like a Ctrl-C) may occasionally
leave the sockets allocated (which *has* happened.)

Later....Jet
 
A

Andrew Dalke

The Jetman:
Thanx ! I *swear* I always spend at least a couple hours or so looking
for stuf like this on my own !

The Python Cookbook is an impressive collection of tidbits.
BTW, I haven't tried it, but I bet os._exit will fail just like
sys.exit().

Unlikely. sys.exit actually raises a Python exception,
SystemExit. This is caught by the XMLPRC server
and converted into a message for the client.

(And this a reason you shouldn't use "except:" in
your code. Though if you really need it, you should
consider
except SystemExit:
raise
except:
...
)

os._exit uses the C function '_exit' which does an exit
right away, without doing the normal cleanups that
exit(3C) does. It doesn't raise an exception and cannot
be overriden by other Python code. (Unless os._exit
was replaced.)
Besides the reason for a graceful shutdown is more
than cosmetic, since a forced shutdown (like a Ctrl-C) may occasionally
leave the sockets allocated (which *has* happened.)

Let me guess, you quit a server program then restarted
it only to get a "cannot rebind to port" error message?

Ctrl-C raises a KeyboardInterrupt exception and its
effect should be the same as SystemExit, except for a
couple very minor issues. It isn't the same as using
os._exit.

What you're seeing is a consequence of the code
not properly handling its network resources. Eg,
it could register an atexit handler which forces
shut all open sockets.

See
http://www.manualy.sk/sock-faq/unix-socket-faq-2.html#time_wait
for a description of what's happening at the TCP level.

Even when that happens, it doesn't "leave the socket
[permanently] allocated" in the system level. If you
wait long enough, the system should release the port.

Andrew
(e-mail address removed)
 
T

The Jetman

Andrew Dalke said:
The Jetman:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/114579

Or you can ungracefully use os._exit, like pulling the emergency
brake on a train.

Andrew
(e-mail address removed)

Andrew:

Thanx ! I *swear* I always spend at least a couple hours or so looking
for stuf like this on my own ! That's the single most frustrating part
about the Web (besides this idiotic W32.Swen virus/worm) ! No matter
how you compose a query, there's always something that you miss....

BTW, I haven't tried it, but I bet os._exit will fail just like
sys.exit(). Besides the reason for a graceful shutdown is more
than cosmetic, since a forced shutdown (like a Ctrl-C) may occasionally
leave the sockets allocated (which *has* happened.)

Later....Jet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top