Shutting down a cross-platform multithreaded app

Chris Angelico rosuav at
Fri Sep 18 23:40:36 CEST 2015

On Sat, Sep 19, 2015 at 3:17 AM, James Harris <james.harris.1 at> wrote:
> Needless to say, on a test Windows machine AF_UNIX is not present. The only
> cross-platform option, therefore, seems to be to use each subthread's
> select()s to monitor two AF_INET sockets: the one to the client and a
> control one from the master thread. I would seem to need IP socket pairs
> between the master thread and the subthreads. If the master thead receives a
> shutdown signal it will send a shutdown command to each subthread.
> The above seems logical but would use quite a few IP sockets. I cannot think
> of a better way, though. Any comments on the ideas above?

If you're using select() to monitor the sockets, you don't actually
then have to _do_ anything with the shutdown socket. You could have a
single socket that sends the shutdown signal to all your workers.

Bear in mind, though, that Windows has no protection against other
processes shutting you down. You can restrict it to (of
course) but any program running on the same computer as the server -
regardless of user permissions etc - will be able to connect to your
sockets. So it might be best to do something like this (all on the
main thread):

1) Open a listening socket
2) Connect to the listening socket
3) Accept a connection
4) Close the original listening socket
5) Spin off all your threads, passing them the socket from step 2
6) To terminate them all, write a byte to the socket from step 3.

This will make it difficult for ordinary userspace code to mess with
you. It'd still be possible, I think, for something with raw sockets
access to feign a termination signal; I have no idea what protections
Windows offers you there.

Give it a shot, see how it goes!


More information about the Python-list mailing list