Rhino7 Commercial Windows crash when I start a websocket server

I want to start a websocket server by running this script in Python Script.
Rhino7 crash when the code runs to the last line
I can’t find any error log

import clr
import System
from System.Threading import Thread, ThreadStart

dll_path = r"E:\PycharmProjects\websocket-sharp\websocket-sharp\bin\Debug\websocket-sharp.dll"
clr.AddReferenceToFileAndPath(dll_path)

from WebSocketSharp.Server import WebSocketServer, WebSocketBehavior

class EchoService(WebSocketBehavior):
def OnMessage(self, e):
self.Send("Echo: " + e.Data)

def start_server():
port = 4649
server = WebSocketServer(port)
server.AddWebSocketServiceEchoService
server.Start()
print(“WebSocket server started at ws://localhost:{}/Echo”.format(port))
while server.IsListening:
System.Threading.Thread.Sleep(1000)

thread = Thread(ThreadStart(start_server))
thread.IsBackground = True
thread.Start()

WebSocket.py (788 Bytes)

Does someone have any idea?

CLR exception type: System.ArgumentNullException
“值不能为 null。”

PROCESS_NAME: Rhino.exe

I open RhinoCrashDump.dmp file with Windbg, and find this

Hi,

Rhino crashes because you run code on a dedicated thread within the Rhino process. You need to try-catch/except whatever you do in this thread, otherwise you will just hard-crash the application on errors. Only on the main(=ui) thread you have this extra guard from Rhino and the script executor.

Hi @Can9 ,
I have been down your path. To save you pain and suffering I think you should expand on something along the lines of

import socket
import System
from System.Threading import Thread, ThreadStart

def socket_server():
    host = "127.0.0.1"
    port = 4649
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((host, port))
    server_socket.listen(1)
    print "Socket server listening {}:{}".format(host, port)
    while True:
        client_socket, address = server_socket.accept()
        print "Accepted connection from", address
        full_code = ("whatever")
        total_sent = 0
        while total_sent < len(full_code):
            sent = client_socket.send(full_code[total_sent:])
            if sent == 0:
                print "Socket  broken"
                break
            total_sent += sent
        client_socket.close())

server_thread = Thread(ThreadStart(socket_server))
server_thread.IsBackground = True
server_thread.Start()

print "Socket serverthread started"

Hope this sparks some ideas,
Farouk

2 Likes

This is not a Websocket! Its a plain TCP socket. This is something different.
Also note that binding fails more often as you might think. Depending on the implementation it either it returns error codes or hard crashes the application on exception (or both). This code isn’t preventing anything from crashing Rhino.

(post deleted by author)

I want to realize MCP in Rhino Refering Blender-MCP. Someone told me it’s a websocket; so I asked GPT how to start a websocket server in Rhino and followed its step.
Nevermind. Your solution is better than GPT’s

OK. I don’t know
I just attemptted to realize MCP in Rhino. Referring Blender-MCP on github. Someone told me it use a websocket but actually a TCP socket

Glad i could help

I don’t know, but in here you see an implementation which uses a plain TCP server which seems to be quite resilient. It basically combines all the information you received here. But you are right, there is no web-socket involved

There are 3 things being mixed up a bit.
1.) Thread-safe coding. Its not a bug of Rhino if you cause un-handled exceptions. Always assume code can fail and on a different thread you need to handle it for yourself.
2.) Knowing and implementing the correct type of socket (A web-socket is also TCP based but has an additional layer. The web-socket header has a strict interface, and while it is technically possible to deal with web-sockets on plain sockets, it is still pointless to re-implement the wheel. A web-socket library might behave differently than the default socket implementation.
3.) Superficially knowing how sockets works, and what can go wrong.
All these topics require advanced coding knowledge. I personally just find it more useful to explain what can go wrong, instead of posting code snippets. So sorry if you expect this from me here, but don’t be demotivated if it overwhelms you. Its difficult.

1 Like

I guess what you want to say is:

  1. Rhino crashed because a error happended in the subprocess
  2. Using WebSocketSharp library is unusual, and misuse of it caused the error in the subprocess.
    I know.

The whole story is that I want to dynamically run Rhino/Grasshopper code generated by a remote LLM service. Building a socket to recieve code is the first step. I want to fast build a runnable one and then build a stable one.

@Can9

Have you seen this video:

It is part of a series of video going over this topic:

Maybe that will point you in a useful direction.

Cheers

DK

1 Like