As I mentioned in my previous post, I am trying to use the rust-websocket
library to implement a prototype debugger server for Servo.

The main server thread needs to wait for one of the following events:
1. An incoming WebSocket connection.
2. A message from the browser

Unfortunately, the former uses a TCP socket, and the latter a mpsc
receiver, and it is not possible to select on both.

To avoid this, we listen for incoming WebSocket connections on a separate
acceptor thread, and then send the connection in a message over the mpsc
channel. This way, the main server thread can wait for both types of event.
Note that this is exactly what we do in the existing devtools server.

One message that the browser should send to the server is a request to
shutdown the server. When the server receives a shutdown request, it should
close all open connections, and then shut down the server.

Unfortunately, as far as I can tell, the rust-websocket library does not
provide a way to shut down a server. This means we have no way to break the
acceptor thread out of its loop; we can't send it a notification, since it
is blocked waiting on an incoming connection. The result is that we end up
leaking the acceptor thread on shutdown.

I can see the following options here. I am leaning towards option 2, but
option 3 is also something we could consider. What are your opinions?

1. Ignore the problem. This is what we do in the existing devtools server.
After all, what is a leaked thread among friends?

2. Write a pull request to add this functionality to rust-websocket. Since
the library uses TCP sockets under the hood, this should be as simple as
closing the listener socket (is it possible to do this from another
thread?). Doing so should break the acceptor thread out of its loop.

3. I found another websocket library for Rust, called "ws". Unlike
rust-websocket, ws seems to have a proper shutdown story (one can call a
shutdown() method on a Sender, which can be cloned between threads). It
also seems to be a bit more robustly designed than rust-websocket, so it
might be worth considering.

On the other hand, like rust-websocket, ws also does not provide a way to
upgrade HTTP requests to WebSocket. And unlike rust-websocket, there
doesn't seem to be a pending pull request to add this functionality, which
means we might have to implement it ourselves.
_______________________________________________
dev-servo mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-servo

Reply via email to