Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for already-listening sockets #192

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shivak
Copy link

@shivak shivak commented Jan 5, 2023

Background. It's often desirable to work off a given listening fd, rather than opening a port or UNIX domain socket. For example, a listening FD can be passed from a process in a global network namespace to a server in a restricted namespace, for security/isolation. In systemd socket activation, the externally-facing network or domain socket is bound and listened by systemd. When a new connection arrives, the service is started with the socket descriptor passed as an int in the LISTEN_FDS environment variable.

Implementation. us_socket_context_listen_direct hastily added without tests or examples. Should probably be renamed, since it takes a listening socket, but doesn't call listen() itself. Just want to get this ball rolling, in the hopes that this mode is eventually supported by Bun. With node, it's possible to run servers directly in this manner. cc @Jarred-Sumner

Adds `us_socket_context_listen_direct`, which takes a socket rather than a port or a path to a UNIX socket.
@Jarred-Sumner
Copy link

Just want to get this ball rolling, in the hopes that this mode is eventually supported by Bun. With node, it's possible to run servers directly in this manner. cc @Jarred-Sumner

This would be nice. The user or API one layer up would need to ensure that the file descriptor isn't already being polled by the current epoll or else it would cause strange behavior, but that's fine

@uNetworkingAB
Copy link
Contributor

Smart, will have a look when I have time

@uNetworkingAB
Copy link
Contributor

It looks small 👍

Simple showcase of passing a socket into a restricted Linux network namespace.
@shivak
Copy link
Author

shivak commented Jan 6, 2023

Yup, just a matter of naming and sanitizing a long-term interface.

I added a small example of how this would be used with network namespaces.

@shivak
Copy link
Author

shivak commented May 1, 2023

@uNetworkingAB, any chance of merging soon? Afterwards, it will be straightforward to add an fd option to Bun's ServeOptions. (Note the similar feature request in Deno: denoland/deno#14008)

@e3dio
Copy link
Contributor

e3dio commented May 31, 2024

I see support for adopting "accepted" sockets via a file descriptor b18d987 but I don't see support for adopting a "listening" socket with app.listen(fd, callback). This is needed for Systemd socket activation, the listening socket FD is passed to the process with connections ready to be accepted. Does this PR still look good or it needs some work?

@shivak
Copy link
Author

shivak commented May 31, 2024

@e3dio , I tested systemd socket activation with the entire stack of changes to Bun and uSockets, since that was the purpose of the PRs. But in the last year, some minor conflicts have arisen. I don’t have time to resolve them right now, but feel free to pick up where I left off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants