closing a server socket

Alex Martelli aleax at aleax.it
Sat Oct 18 13:38:09 EDT 2003


simon place wrote:
   ...
> Yes, no problem closing a server before it returns to accept another
> connection, but when the handler is threaded,

When the server is threaded, it should be designed accordingly; see
Python's standard library, module SocketServer etc, for one right
way to design a threaded server.

> i.e.  closing server socket does not unblock accept and you can still get
> connections on it!
> ( because it has not actually been closed!)
> how is this acceptable?

It's not: in general, two threads should NEVER access the same 
resource simultaneously, unless the resource is specifically 
known to be thread safe.  sockets aren't.  It's not acceptable,
and the bug is in the program which has one of its threads
make a sock.close() call while another is waiting on a
sock.accept() on the same socket sock.


> with socket modified to actually close the socket,
   ...
> i.e. close socket generates an exception on accept and no more
> connections.

On _your_ platform, no doubt.  I'd bet substantial money that,
without even going to especially _exotic_ platforms, you'll find
terrible misbehavior with such mods on at least one of the
platforms I routinely program for.


> If there's some underlying difficulty that causes this to come back and
> bite the testing i've done shows this to take a very long time.

Just as much time as is necessary to go and purchase some
other platform to try it on, I suspect.


Alex





More information about the Python-list mailing list