Sublime Forum

Running a web server with a plugin crashes Sublime

#1

I’m new to Sublime plugins, and I’m trying to create a plugin that calls a function to create a socketserver. However, when I try to run the command from console it ends up crashing sublime, and I have to force-quit it. Is there any way to stop the crashing from happening? The server works fine when I build it in a separate file.

class AuthenticateCommand(sublime_plugin.TextCommand):

def run(self, edit, should_authenticate=False):
	run_server()

class MyTCPHandler(socketserver.BaseRequestHandler):

def handle(self):
    (put my handle code here)

def run_server():

HOST, PORT = "localhost", 8001

server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
print("Server created")

try:
    print("Serving server")
    server.handle_request()
    server.server_close()
except:
    server.server_close()

I noticed that if I called run_server from outside and reloaded the plugin with cmd-S, Sublime would crash but the server would be running after I reopened it again, so I’m a little confused. I’m currently using Sublime Text 3 on MacOSX.

0 Likes

#2

If you have to force quit, then it’s not crashing, it’s hanging. Note that when you run a command, Sublime doesn’t regain control from the plugin host until after the command finishes executing. This seems like what you’re experiencing here.

Basically, unless run_server() implicitly starts a background thread and then returns, it’s going to hang Sublime forever because the command never finishes. You can get around that by specifically starting a new thread and invoking run_server() from within it.

2 Likes