Rocket icon indicating copy to clipboard operation
Rocket copied to clipboard

Allow `launch_on` with a Tokio Unix socket listener

Open thetorpedodog opened this issue 1 year ago • 2 comments

What's missing?

Currently (v0.6 development), you can pass a Tokio TcpListener to launch_on. However, you can’t do the same thing with a UnixListener. Doing this would bring parity between Rocket’s internal support for unix:/path/to/whatever–style addresses and what launch_on accepts.

This would make it possible for external users (e.g., me) to use Rocket with systemd socket activation, where an already-bound socket is passed into the process. Note that this already works with TCP sockets—you can construct a tokio::net::TcpListener from the passed-in socket and provide it to Rocket.

This is a continuation of #545 and #2485.

Ideal Solution

Unlike the TcpListener type, which is directly used from Tokio, there is a custom UnixListener struct which handles creation of the socket file, locking, etc.

To this we add a From<tokio::net::UnixListener implementation which simply takes ownership of the Tokio listener, and make the path also Optional (so we don’t try to remove it upon drop).

impl From<tokio::net::UnixListener> for UnixListener {
    fn from(listener: tokio::net::UnixListener) {
        Self{ path: None, lock: None, listener }
    }
}

Why can't this be implemented outside of Rocket?

There is no way to pass your own Unix socket into Rocket otherwise.

Are there workarounds usable today?

No response

Alternative Solutions

No response

Additional Context

No response

System Checks

  • [x] I do not believe that this feature can or should be implemented outside of Rocket.
  • [x] I was unable to find a previous request for this feature.

thetorpedodog avatar Apr 20 '25 21:04 thetorpedodog

An alternate, though broadly equivalent, solution would be to rename Rocket’s special UnixListener to something like OwnedUnixListener or ManagedUnixListener and add an impl Listener for UnixListener block (now referring to Tokio). I kind of like this more, since it discerns between Tokio UnixListeners and UnixListeners we bind and own ourselves and have to clean up, though it does have further-reaching code changes.

thetorpedodog avatar Apr 21 '25 23:04 thetorpedodog

add an impl Listener for UnixListener block

I think we should change the existing rocket::listener::unix::UnixListener to LockingUnixListener (or one of your names), remove the reuse parameter, and make lock non-optional. Additionally, we should call out the difference in the docks, since Unix sockets can have some surprising behavior when they aren't cleaned up. After doing some research, it looks like you might need to set up some additional code to ensure your sockets are cleaned up, since it looks like systemd doesn't handle removing the socket when you're done.

I would accept a PR to make this change.

the10thWiz avatar May 25 '25 20:05 the10thWiz