Socket cannot close from script

This is probably a long-shot, but I’m hoping someone here may have some advice or workaround.

I have a rhinopython script that opens a modeless eto window and a socket server on another thread. Control and communication are via two Queue.Queue(). The general idea is the window is just for display/feedback and stopping the server/script when user wants to. External programs connect a socket to the server and send messages.

Everything works fine on the first run of the script, on a fresh start of Rhino. If I stop the script, everything seems to shut down correctly…no errors or exceptions that can see. However, upon second start of the script, no clients can connect to the server.

I have tried 2 approaches for the socketry:

  1. Using non-blocking socket.socket()

  2. Using SocketServer.TCPServer()

  3. results in multiple listening handles holding the socket, according to netstat -ano|findstring

  4. results in [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted, since I can’t seem to get the reuse option set to true.

Method 2 is more overhead than I want, and isn’t solving anything for me.

Charging on with method 1, I duplicated the code as much as possible in cpython and observe with netstat that the listening handle does not linger as it is in rhino. Curiously, I also observe if I wait long enough, the listening handles from rhino.exe will disappear on their own, and clients can then connect after I start the script again.

I have taken great care to .close() all client connections after receiving data, as well as the serving socket, upon exiting of the eto window (on_form_close event). The thread is join()ed after socket is closed.

I have a feeling that the lingering listening handle exists because Rhino.exe (that is the PID associated) is still running. The only other thing I can think to try is to use .net socket/thread instead of the ironpython implementations, but haven’t been able to google up any information that may suggest that will change anything.

I can send some minimal code if anyone would like to take a look.


1 Like

I quick search on my favourite search engine yielded

Maybe this is something that works for you too?

Yeah I googled to that as well at some point…I’m not using the makefile function though…knowingly anyway.