#4: Paster restart socket error

After saving source files (with --reload option activated), sometimes paster can't restart and encounters the below error.

Mac OS 10.5

-------------------------


Starting server in PID 58828.
Traceback (most recent call last):
File "/usr/local/bin/paster", line 8, in <module>
load_entry_point('PasteScript==1.6.3', 'console_scripts', 'paster')()
File "/Library/Python/2.5/site-packages/PasteScript-1.6.3-py2.5.egg/paste/script/command.py", line 79, in run
invoke(command, command_name, options, args[1:])
File "/Library/Python/2.5/site-packages/PasteScript-1.6.3-py2.5.egg/paste/script/command.py", line 118, in invoke
exit_code = runner.run(args)
File "/Library/Python/2.5/site-packages/PasteScript-1.6.3-py2.5.egg/paste/script/command.py", line 213, in run
result = self.command()
File "/Library/Python/2.5/site-packages/PasteScript-1.6.3-py2.5.egg/paste/script/serve.py", line 260, in command
server(app)
File "/Library/Python/2.5/site-packages/PasteDeploy-1.3.2-py2.5.egg/paste/deploy/loadwsgi.py", line 151, in server_wrapper
**context.local_conf)
File "/Library/Python/2.5/site-packages/PasteDeploy-1.3.2-py2.5.egg/paste/deploy/util/fixtypeerror.py", line 57, in fix_call
val = callable(*args, **kw)
File "/Library/Python/2.5/site-packages/Paste-1.7.1-py2.5.egg/paste/httpserver.py", line 1317, in server_runner
serve(wsgi_app, **kwargs)
File "/Library/Python/2.5/site-packages/Paste-1.7.1-py2.5.egg/paste/httpserver.py", line 1267, in serve
threadpool_options=threadpool_options)
File "/Library/Python/2.5/site-packages/Paste-1.7.1-py2.5.egg/paste/httpserver.py", line 1117, in __init__
RequestHandlerClass, ssl_context)
File "/Library/Python/2.5/site-packages/Paste-1.7.1-py2.5.egg/paste/httpserver.py", line 1097, in __init__
RequestHandlerClass, ssl_context)
File "/Library/Python/2.5/site-packages/Paste-1.7.1-py2.5.egg/paste/httpserver.py", line 371, in __init__
self.server_bind()
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/BaseHTTPServer.py", line 101, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 341, in server_bind
self.socket.bind(self.server_address)
File "<string>", line 1, in bind
socket.error: (48, 'Address already in use')

a potential bug in Paste

1
Try adding:

self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

to /Library/Python/2.5/site-packages/Paste-1.7.1-py2.5.egg/paste/httpserver.py, line 360, immediately after this line:
self.socket = socket.socket(self.address_family, self.socket_type)

This sets the REUSEADDR flag on the socket, which allows it to keep the original port it started out with.

Force all sockets to use the REUSEADDR flag

1
....alternatively, you could go into /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py, line 323:

allow_reuse_address = False

and set it to True instead.

This will then affect line 341:
if self.allow_reuse_address: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

and force the application of the SO_REUSEADDR flag to any socket. This changes the fundamental way socket servers behave in Python, so it's kind of heavy-handed.

Think you've got a better solution? Help Thumb_cartoon_futurama_flexo_small hannes out by posting your solution