Skip to content

Commit

Permalink
Cleanly shut down server socket
Browse files Browse the repository at this point in the history
This decreases or eliminates the prevalence of `Address already in use`
errors when the server is killed (e.g. via a `KeyboardInterrupt`) and
immediately restarted.

Also, added some log messages.
  • Loading branch information
lpsinger committed Sep 14, 2015
1 parent 36c512b commit 7b5f2b4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion gcn/voeventclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,20 @@ def serve(payloads, host='127.0.0.1', port=8099, retransmit_timeout=0, log=None)
except socket.error:
log.exception('error communicating with peer')
finally:
try:
conn.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', 1, 0))
except socket.error:
log.exception('could not prepare to reset socket')
else:
log.info('prepared to reset socket')

try:
conn.shutdown(socket.SHUT_RDWR)
except socket.error:
log.exception("could not shut down socket")
else:
log.info("shut down socket")

try:
conn.close()
Expand All @@ -303,4 +313,9 @@ def serve(payloads, host='127.0.0.1', port=8099, retransmit_timeout=0, log=None)
else:
log.info("closed socket")
finally:
sock.close()
try:
sock.close()
except socket.error:
log.exception('could not close listening socket')
else:
log.info("closed listening socket")

0 comments on commit 7b5f2b4

Please sign in to comment.